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