0% found this document useful (0 votes)
139 views

PDBC

The document provides code examples for connecting to a MySQL database and performing basic operations using Python and the mysql-connector-python module. It shows how to import the module, connect to a MySQL database, execute queries to retrieve and insert data, and close the connection.

Uploaded by

sekhar s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

PDBC

The document provides code examples for connecting to a MySQL database and performing basic operations using Python and the mysql-connector-python module. It shows how to import the module, connect to a MySQL database, execute queries to retrieve and insert data, and close the connection.

Uploaded by

sekhar s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PDBC

How to Connect to MySQL Database in Python

1. Install MySQL connector module

Use the pip command to install MySQL connector Python.

pip install mysql-connector-python

2. Import MySQL connector module

Import using a import mysql.connector statement so We can use


this module’s methods to communicate with the MySQL database.

import mysql.connector

from mysql.connector import Error

def connect():

""" Connect to MySQL database """

conn = None

try:

conn = mysql.connector.connect(host='localhost',

database='crud',

user='root',

password='root')

if conn.is_connected():

print('Connected to MySQL database')

except Error as e:

print(e)
finally:

if conn is not None and conn.is_connected():

conn.close()

if __name__ == '__main__':

connect()

Fetch:

import mysql.connector

try:

connection = mysql.connector.connect(host='localhost',
user='root',database='crud1',password='root')

sql_select_Query = "select * from myapp_usersmodel"

cursor = connection.cursor()

cursor.execute(sql_select_Query)

# get all records

records = cursor.fetchall()

print("Total number of rows in table: ", cursor.rowcount)

print("\nPrinting each row")

for row in records:

print("Id = ", row[0], )

print("Name = ", row[1])

print("email = ", row[3])

print("mobile = ", row[4], "\n")


except mysql.connector.Error as e:

print("Error reading data from MySQL table", e)

finally:

pass

import mysql.connector

try:

connection = mysql.connector.connect(host='localhost',

database=enp,

user=root,

password='root')

mySql_insert_query = """INSERT INTO emp (Id, Name, sal, joingi_date)

VALUES

(15, raj, 6459, '2019-08-14') """

cursor = connection.cursor()

cursor.execute(mySql_insert_query)

connection.commit()

print(cursor.rowcount, "Record inserted successfully into user table")

cursor.close()

except mysql.connector.Error as error:

print("Failed to insert record into user table {}".format(error))

finally:

if connection.is_connected():

connection.close()
print("MySQL connection is closed")

You might also like