Tuple
Tuple
Computer Science
Python Database
Connectivity
Session-1
• Import
mysql.connector/PyMySQL
module
• Create the connection object
• Create the cursor object
• Execute the query
whether
connector
2. Type import
is mysql.connector/import pymysql
properly
installed
or not? 3. If no error occurs, it means
connector works
mydb = mysql.connector.connect(
<mysql.connector.connection_cext.CMy
host="localhost",
SQLConnection object at
user="yourusername",
0x7fa26d368390>
password="yourpassword"
)
print(mydb)
Deepshikha Sethi----XII Computer Science -----AIS Mayur Vihar
Cursor in python
• Cursors are created by the connection.cursor( ) method
• they are bound to the connection for the entire lifetime of the program
• all the commands are executed the context of the database session wrapped by
connection.
<cur_obj> = conn.cursor()
for x in Mycur:
print(x) # Fetching database from Mycur one by one
conn.close()
Showing list of
databases
Deepshikha Sethi----XII Computer Science -----AIS Mayur Vihar
Create database through python
import pymysql as pm while True:
#Creating the connection Object print("1. Create Database")
op=int(input("Enter your choice"))
conn=pm.connect(host="localhost", user="root", passwd=“mysql") if op==1:
#Creating the cursor object
Create create_Database()
else:
Mycur = conn.cursor()
database break
def create_Database():
Mycur.execute("create database Directory") #Creating Database Directory conn.close()
Mycur.execute("show databases")