This document contains Python code to connect to a MySQL database, fetch data from a movie table using different fetch methods, and insert new data. It connects to the database, executes a SELECT statement to retrieve all rows from the movie table, and uses fetchone, fetchmany, and fetchall to retrieve the data in different ways, printing the row counts. It then shows how to fetch data using fetchmany and print the rows. Finally, it demonstrates how to connect and prepare to insert new data into the database.
This document contains Python code to connect to a MySQL database, fetch data from a movie table using different fetch methods, and insert new data. It connects to the database, executes a SELECT statement to retrieve all rows from the movie table, and uses fetchone, fetchmany, and fetchall to retrieve the data in different ways, printing the row counts. It then shows how to fetch data using fetchmany and print the rows. Finally, it demonstrates how to connect and prepare to insert new data into the database.
This document contains Python code to connect to a MySQL database, fetch data from a movie table using different fetch methods, and insert new data. It connects to the database, executes a SELECT statement to retrieve all rows from the movie table, and uses fetchone, fetchmany, and fetchall to retrieve the data in different ways, printing the row counts. It then shows how to fetch data using fetchmany and print the rows. Finally, it demonstrates how to connect and prepare to insert new data into the database.
mycon=sqltor.connect(host="localhost",user="root",passwd="mysqlprachi16#",database= "prachi") if mycon.is_connected()==False: print('error connecting to mysql databse') cursor=mycon.cursor() cursor.execute('select*from movie') data= cursor.fetchone() count=cursor.rowcount print("total number of rows retrived so far from resultset",count) data=cursor.fetchone() count=cursor.rowcount print("total number of rows retrived so far from resultset",count) data=cursor.fetchmany(3) count=cursor.rowcount print("total number of rows retrived so far from resultset",count) data=cursor.fetchall() count=cursor.rowcount print("total number of rows retrived so far from resultset",count)
#to fetch data
import mysql.connector as sqltor mycon=sqltor.connect(host="localhost",user="root",passwd="mysqlprachi16#",database= "prachi") #if mycon.is_connected()==False:
#print('error connecting to mysql databse')
cursor=mycon.cursor() cursor.execute('select * from movie') data=cursor.fetchmany(3) count=cursor.rowcount for row in data: print(row) mycon.close()
#to insert data
import mysql.connector as sqltor mycon=sqltor.connect(host="localhost",user="root",passwd="mysqlprachi16#",database= "prachi") #if mycon.is_connected()==False: #print('error connecting to mysql databse') cursor=mycon.cursor()