Python session 10

15
WELCOME TO PYTHON SESSION 10 WELCOME TO PYTHON SESSION 10

description

Python -DB connectivity

Transcript of Python session 10

Page 1: Python session 10

WELCOME TO PYTHON SESSION 10WELCOME TO PYTHON SESSION 10

Page 2: Python session 10

AGENDA

Database Connection

What is Cursor.

What is the uses of MySQLdb in python.

Page 3: Python session 10

Database Connection

Page 4: Python session 10

Creating Database Table

Page 5: Python session 10

Insert Operation

Page 6: Python session 10

Delete Operation

Page 7: Python session 10

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

Example

Page 9: Python session 10

Inserting Images

Page 10: Python session 10

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

Example

Page 12: Python session 10

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

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

THANK YOU

Page 15: Python session 10

Reference

Reference Link:

http://www.tutorialspoint.com/python/python_database_access.htm