Python Database Connectivity
Python Database Connectivity
CONNECTIVITY
Database Connection
1.Import mysql.connector module
2.Create the connection object.
3.Create the cursor object
4.Execute the query
Creating the connection
import mysql.connector
Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",pas
swd = "google")
#printing the connection object
print(myconn)
Argument Description
Username The username that you use to work with MySQL Server. The default
username for the MySQL database is a root.
Password Password is given by the user at the time of installing the MySQL server. If
you are using root then you won’t need the password.
Host name The server name or Ip address on which MySQL is running. if you are
running on localhost, then you can use localhost or its IP 127.0.0.0
Database name The name of the database to which you want to connect and perform the
operations.
Creating a cursor object
• create the cursor object by calling the 'cursor' function of the connection object.
import mysql.connector
#Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"google", database = "mydb")
#printing the connection object
print(myconn)
#creating the cursor object
= myconn.cursor()
pcurrint(cur)
Creating Tables
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "yourusername",
password = "your_password",
database = “mydatabase"
)
cursor = mydb.cursor()
cursor.execute("CREATE TABLE gfg (name VARCHAR(255), user_name VARCHAR(255))")
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
Insert a Single Row into MySQL table
• INSERT INTO mysql_table (column1, column2, …)
VALUES (value1, value2, …);
Insert multiple rows into MySQL table
using the cursor’s executemany()
• Syntax
• cursor.executemany(operation, seq_of_params)
• INSERT INTO Laptop (Id, Name, Price, Purchase_date)
VALUES (%s, %s, %s, %s)
• records_to_insert = [(4, 'HP Pavilion Power', 1999,
'2019-01-11'), (5, 'MSI WS75 9TL-496', 5799, '2019-
02-27'), (6, 'Microsoft Surface', 2330, '2019-07-
23')]
mport mysql.connector
try:
connection = mysql.connector.connect(host='localhost',
database='Electronics',
user='pynative',
password='pynative@#29')
mySql_insert_query = """INSERT INTO Laptop (Id, Name, Price, Purchase_date)
VALUES (%s, %s, %s, %s) """
records_to_insert = [(4, 'HP Pavilion Power', 1999, '2019-01-11'),
(5, 'MSI WS75 9TL-496', 5799, '2019-02-27'),
(6, 'Microsoft Surface', 2330, '2019-07-23')]
cursor = connection.cursor()
cursor.executemany(mySql_insert_query, records_to_insert)
connection.commit()
print(cursor.rowcount, "Record inserted successfully into Laptop table") #find the number of records inserted
except mysql.connector.Error as error:
print("Failed to insert record into MySQL table {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")