DEV Community

Lucas Silva
Lucas Silva

Posted on

Connect Python with MySQL database ( mysql-connector )

Training my english !!!
Hello, my name is Lucas. I be writing this artigle for sharing an code for connect python with database MySQL.

For connecting python in database mysql, we require install an library ( mysql-connector ). For install this library, you can write in your concolse the command(using PIP):

pip install mysql-connector-python
Enter fullscreen mode Exit fullscreen mode
  1. With library instaling, we can initialize the create code for connection. In your VSCode, create an file with name "app.py";
  2. In your file "app.py" import the library ( mysql-connector ) writing the this line:
import mysql.connector
Enter fullscreen mode Exit fullscreen mode
  1. In your file "app.py" write the code with data the connectio MySQL
mydb = mysql.connector.connect(
    host='',
    user='',
    password='',
    database=''
)

Enter fullscreen mode Exit fullscreen mode
  • host: the host of connection in database MySQL;
  • user: username of connection in database;
  • password: password of connection in database;
  • database: name of your database

For finality the code, we have to write the line of conection:

con = mydb.cursor()
Enter fullscreen mode Exit fullscreen mode

Your code should look like this:

import mysql.connector

mydb = mysql.connector.connect(
    host='',
    user='',
    password='',
    database=''
)

con = mydb.cursor()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)