python session 10
DESCRIPTION
Python -DB connectivityTRANSCRIPT
![Page 1: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/1.jpg)
WELCOME TO PYTHON SESSION 10WELCOME TO PYTHON SESSION 10
![Page 2: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/2.jpg)
AGENDA
Database Connection
What is Cursor.
What is the uses of MySQLdb in python.
![Page 3: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/3.jpg)
Database Connection
![Page 4: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/4.jpg)
Creating Database Table
![Page 5: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/5.jpg)
Insert Operation
![Page 6: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/6.jpg)
Delete Operation
![Page 7: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/7.jpg)
Read Operation Once our database connection is established, we are ready to make a query into this database.
Fetchone():
This method fetches the single row of a query result set. A result set is an object that is returned when a cursor object is used to query a table.
Fetchall():
This method fetches all the rows in a result set. If some rows have already been extracted from the result set, the fetchall() method retrieves the remaining rows from the result set.
Rowcount:
This is a read-only attribute and returns the number of rows that were affected by an execute() method.
![Page 8: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/8.jpg)
Example
![Page 9: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/9.jpg)
Inserting Images
![Page 10: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/10.jpg)
The dictionary cursor
There are multiple cursor types in the MySQLdb module.
The default cursor returns the data in a tuple of tuples.
When we use a dictionary cursor, the data is sent in a form
of Python dictionaries. This way we can refer to the data by
their column names.
![Page 11: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/11.jpg)
Example
![Page 12: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/12.jpg)
Connection ObjectsCOMMIT Operation:
Commit is the operation, which gives a green signal to database to finalize the changes, and after this operation, no change can be reverted back
db.commit()
ROLLBACK Operation:
If you are not satisfied with one or more of the changes and you want to revert back those changes completely, then use rollback() method.
db.rollback()
![Page 13: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/13.jpg)
Connection ObjectsCOMMIT Operation:
Commit is the operation, which gives a green signal to database to finalize the changes, and after this operation, no change can be reverted back
db.commit()
ROLLBACK Operation:
If you are not satisfied with one or more of the changes and you want to revert back those changes completely, then use rollback() method.
db.rollback()
![Page 14: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/14.jpg)
THANK YOU
![Page 15: Python session 10](https://reader036.vdocuments.site/reader036/viewer/2022062307/5552c1aeb4c90581158b4800/html5/thumbnails/15.jpg)
Reference
Reference Link:
http://www.tutorialspoint.com/python/python_database_access.htm