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

Python Database Programming 03

The document outlines the use of a Cursor object in Python for executing SQL queries, including methods like execute, executescript, and executemany. It explains how to commit or rollback changes for DML queries and how to fetch results using fetchone, fetchall, and fetchmany. Finally, it emphasizes the importance of closing resources after operations are completed.

Uploaded by

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

Python Database Programming 03

The document outlines the use of a Cursor object in Python for executing SQL queries, including methods like execute, executescript, and executemany. It explains how to commit or rollback changes for DML queries and how to fetch results using fetchone, fetchall, and fetchmany. Finally, it emphasizes the importance of closing resources after operations are completed.

Uploaded by

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

cursor=con.

cursor()

4. Execute SQL Queries By using Cursor object. For this we can use the following methods

i) execute(sqlquery)  To execute a single sql query


ii) executescript(sqlqueries)  To execute a string of sql queries seperated by semi-colon ';'
iii) executemany()  To execute a Parameterized query

Eg: cursor.execute("select * from employees")

5. commit or rollback changes based on our requirement in the case of DML


Queries(insert|update|delete)

commit()  Saves the changes to the database


rollback()  rolls all temporary changes back

6. Fetch the result from the Cursor object in the case of select queries
fetchone()  To fetch only one row
fetchall()  To fetch all rows and it returns a list of rows
fecthmany(n)  To fetch first n rows

Eg 1: data =cursor.fetchone()
print(data)

Eg 2: data=cursor.fetchall()
for row in data:
print(row)

7. close the resources


After completing our operations it is highly recommended to close the resources in the reverse
order of their opening by using close() methods.

cursor.close()
con.close()

Note: The following is the list of all important methods which can be used for python database
programming.

connect()
cursor()
execute()
executescript()
executemany()
commit()
rollback()
fetchone()
fetchall()
fetchmany(n)

nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com

You might also like