How to Create a Connection to Database in Python


In this example we will show how to create a connection to the database in Python.

Source Code

import mysql.connector

# connect to server
mydb = mysql.connector.connect(
    host="localhost",
    port=3306,
    user="yourusername",
    password="yourpassword"
)

print(mydb)

Output:

<mysql.connector.connection.MySQLConnection object at 0x015EA150>

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments