Python and Mysql Connectivity Class 12 SN
Python and Mysql Connectivity Class 12 SN
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,..), …
]
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: