13
Python MySQL Database Access

Python MySQL Database Access. Databases: an Introduction A database is a program that manages a set of data Most databases are relational SQL databases

Embed Size (px)

Citation preview

Page 1: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Python MySQL Database Access

Page 2: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Databases: an IntroductionDatabases: an Introduction A database is a program that manages a set of dataA database is a program that manages a set of data Most databases are relational SQL databasesMost databases are relational SQL databases SQL – Structured Query Language, a standardized SQL – Structured Query Language, a standardized

syntax for telling a database what to dosyntax for telling a database what to do Databases take some sort of connection as Databases take some sort of connection as

communication – usually through TCP/IPcommunication – usually through TCP/IP Most databases are set up as a group of schemas, each Most databases are set up as a group of schemas, each

containing a set of tablescontaining a set of tables Each table contains a set of columns, each with a Each table contains a set of columns, each with a

different data typedifferent data type Rows are the entries in a tableRows are the entries in a table

Page 3: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Mysql and PythonMysql and Python You must download a separate DB API module for each You must download a separate DB API module for each

database you need to access.database you need to access. The DB API provides a minimal standard for working The DB API provides a minimal standard for working

with databases, using Python structures and syntax with databases, using Python structures and syntax wherever possible. This API includes the following:wherever possible. This API includes the following:

Importing the api module.Importing the api module. Acquiring a connection with the database.Acquiring a connection with the database. Issuing SQL statements and stored procedures.Issuing SQL statements and stored procedures. Closing the connectionClosing the connection

MySQL is the most widely used open-source database – MySQL is the most widely used open-source database – Python supports it!Python supports it!

Uses library Uses library MySQLdb MySQLdb – a mySQL API for Python– a mySQL API for Python Must install this yourself – on Linux with a package Must install this yourself – on Linux with a package

managermanager

Page 4: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

What is MySQLdb?What is MySQLdb? MySQLdb is an interface for connecting to a MySQL database server from Python. MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0, and is built on top of the MySQL C API.It implements the Python Database API v2.0, and is built on top of the MySQL C API. To install MySQLdb module, download it from MySQLdb Download page and proceed as follows:To install MySQLdb module, download it from MySQLdb Download page and proceed as follows: For fedoraFor fedora

For ubuntuFor ubuntu sudo apt-get install python2.7-mysqldbsudo apt-get install python2.7-mysqldb

Page 5: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

MySQL using XAMPPMySQL using XAMPP #/opt/lampp/bin #./mysql Now u should be able to connect and u will see a welcome message

and MySQL prompt: mysql> you can start creating your database as follows:

To verify that the database was created

Page 6: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

MySQL Contd….MySQL Contd….

At this time you are ready to start creating tables in the database

mysql> create table student (name varchar (30), age integer(2));To Enter data into the tablemysql> insert into student values (‘Asha’,23);To Retrieve the entered datamysql>select * from student;View the parameters of the created table, as follows:mysql> describe studentTo view the table structure you created, enter the following commandmysql>show tables

To delete the entry in the tablemysql>delete from student where age=30

Page 7: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Example-1 Example-1 Following is the example of connecting with MySQL databaseFollowing is the example of connecting with MySQL database

Page 8: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Example-2 Example-2

Page 9: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Insert data into the databaseInsert data into the database

Page 10: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Following code segment is another form of Following code segment is another form of execute where you can pass parameters execute where you can pass parameters directly:directly:

Page 11: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Read OperationRead Operation READ Operation on any database means to fetch some READ Operation on any database means to fetch some

useful information from the database.useful information from the database. fetchone(): fetchone(): This method fetches the next row of a query This method fetches the next row of a query

result set. A result set is an object that is returned when result set. A result set is an object that is returned when a cursor object is used to query a table.a cursor object is used to query a table.

fetchall(): fetchall(): This method fetches all the rows in a result This method fetches all the rows in a result set. If some rows have already been extracted from the set. If some rows have already been extracted from the result set, the fetchall() method retrieves the remaining result set, the fetchall() method retrieves the remaining rows from the result set.rows from the result set.

rowcount: rowcount: This is a read-only attribute and returns the This is a read-only attribute and returns the number of rows that were affected by an number of rows that were affected by an execute()method.execute()method.

Page 12: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

Update and DeleteUpdate and Delete UPDATE Operation on any database means to UPDATE Operation on any database means to

update one or more records which are already update one or more records which are already available in the database.available in the database.

DELETE operation is required when you want to delete DELETE operation is required when you want to delete some records from your database. Following is the some records from your database. Following is the procedure to delete all the records from EMPLOYEE procedure to delete all the records from EMPLOYEE

where AGE is more than 20where AGE is more than 20..

Page 13: Python MySQL Database Access. Databases: an Introduction  A database is a program that manages a set of data  Most databases are relational SQL databases

ExercisesExercises Create a new Database called Create a new Database called ‘FinalYears’‘FinalYears’. Create a . Create a

Table ‘Table ‘Student’ Student’ with with ‘USN’,’Name’‘USN’,’Name’ and and ‘Age’‘Age’ as fields. as fields. Write a program to insert your Write a program to insert your USN, NameUSN, Name and and AgeAge

information into the table from a web pageinformation into the table from a web page.. Create a new database called ‘Movies’. Create a table Create a new database called ‘Movies’. Create a table

called ‘HindiFilms’ with fields ‘Name’, ‘Budget’, ‘Hero’ called ‘HindiFilms’ with fields ‘Name’, ‘Budget’, ‘Hero’ and ‘Heroine’. Write a program to accept these fields and ‘Heroine’. Write a program to accept these fields information from a web page and to store those in the information from a web page and to store those in the table. table.

Program to search a movie for a title given by the user Program to search a movie for a title given by the user on a web page and display the search results with on a web page and display the search results with proper headings.proper headings.