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

Python and Mysql Connectivity Class 12 SN

The document provides a guide on connecting Python with MySQL, including commands for creating databases, tables, and inserting values. It outlines the syntax for executing various operations such as showing databases, inserting single and multiple records, and displaying table contents. Additionally, it explains methods to fetch records from a table, including fetchall, fetchmany, and fetchone.

Uploaded by

pranavrao168
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python and Mysql Connectivity Class 12 SN

The document provides a guide on connecting Python with MySQL, including commands for creating databases, tables, and inserting values. It outlines the syntax for executing various operations such as showing databases, inserting single and multiple records, and displaying table contents. Additionally, it explains methods to fetch records from a table, including fetchall, fetchmany, and fetchone.

Uploaded by

pranavrao168
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python and MySQL

25 July 2023 18:46

All the commands are supposed to be executed in python file only.

To connect Python and MySQL:


Syntax:
import mysql.connector as con
mydb = con.connect(host='localhost',user='root', password='youtreatedmelikeshitlol05')
cur = mydb.cursor()

To show all Databases:


Syntax:
cur.execute('Show Databases')
for i in cur:
print(i)

To create a Database and Select the Database:


Syntax:
cur.execute('create Database python') // cur.execute('create Database <database_name>')
cur.execute('use python') // cur.execute('use <database_name>')
To create a Table:
Syntax:
cur.execute('create table <table_name> (<table_definition>)')

Example: cur.execute('create table student (id int primary key, name varchar(255),class int, sec char(1))')
To Insert Values:
For entering single tuple values:
Syntax:
sql = 'insert into <table_name>(column1,column2,column3,…) values (%s,%s,%s,...)'
val = (value1,value2,value3,…)
cur.execute(sql,val)
mydb.commit()
Example:
sql = 'insert into student (id, name, class, sec) values (%s,%s,%s,%s)'
val = (1,'John',12,'A')
cur.execute(sql,val)
mydb.commit()
For entering multiple tuple values:
Syntax:
sql = 'insert into <table_name>(column1,column2,column3,…) values (%s,%s,%s,...)
val = [
(value1,value2,value3,..),
(value1,value2,value3,..),
(value1,value2,value3,..), …
]

Python and MySQL Page 1


Example:
sql = 'insert into <table_name>(column1,column2,column3,…) values (%s,%s,%s,...)
val = [
(6, 'Peter',12,'C'),
(7, 'Roy',12,'D'),
(8, Whitaker',12,'A'),]
cur.executemany(sql,val)
mydb.commit()
To Display everything in a table:
Syntax:
cur.execute('select * from <table_name>')
myresult = cur.fetchall()
for i in myresult:
print(i)
Example:
cur.execute('select * from empdetails')
myresult = cur.fetchall()
for i in myresult:
print(i)

Methods to Display record(s) in a table:


1. fetchall():This functions fetches all the rows of the query result. It returns all the rows as a list of
tuples an empty list is returned if there is no record to fetch.
Example:

Output:

2. fetchmany(size): This function returns the number of rows specified by size, when called repeatedly
this method fetches the next set of rows of the query result and returns a list of tuples. If no more
rows available it returns an empty list.
Example:

Output:

3. fetchone(): This function returns a single record or none if no more rows are available.
Example:

Python and MySQL Page 2


Output:

Python and MySQL Page 3

You might also like