0% found this document useful (0 votes)
23 views1 page

Connectivity 1

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.

Uploaded by

prachi jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Connectivity 1

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.

Uploaded by

prachi jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#to connect

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.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()

You might also like