connect python to mysql 1

connect python to mysql

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  port = 8888, #for Mamp users
  database='whatever db you want'
)
print(mydb) 

Here is what the above code is Doing:
1. We’re importing the mysql.connector module.
2. We’re creating a connection object (mydb) that will connect to the database.
3. We’re using the connect() method to connect to the database.
4. We’re passing the connect() method 4 arguments:
a. host – this is the IP address of the database server.
b. user – this is the username that you use to log in to the database server.
c. password – this is the password that you use to log in to the database server.
d. database – this is the name of the database that you want to work with.
5. We’re printing the connection object to see if it was successfully created.

Similar Posts