Python

How to connect mysql database in Python

PYTHON_SQL_CONNECT

Python needs a MySQL driver to access the MySQL database, in case this driver is not installed you can install “MySQL Connector”. by using PIP.

Once  “MySQL Connector” is installed, you can use the code below to connect your mysql database

import mysql.connector

mydb = mysql.connector.connect(
  host=“localhost”,
  user=dbrname,
  passwd=dbpassword
)

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = mydb.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM MY_TABLE_NAME")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row[0]

mydb.close()

Comment here

8 + 1 =