Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI/CMPE 4341 Topic: Programming in Python Chapter 10: Database Application Programming Interface Xiang Lian The University of Texas – Pan American Edinburg,

Similar presentations


Presentation on theme: "CSCI/CMPE 4341 Topic: Programming in Python Chapter 10: Database Application Programming Interface Xiang Lian The University of Texas – Pan American Edinburg,"— Presentation transcript:

1 CSCI/CMPE 4341 Topic: Programming in Python Chapter 10: Database Application Programming Interface Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu 1

2 Objectives In this chapter, you will: – Understand relational database model – Learn basic database SQL queries – Use packages to create and query a database 2

3 Introduction A database is an organized collection of data A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data Existing DBMSs – Microsoft SQL Server – Oracle – Sybase – IBM DB2 3

4 Introduction (cont'd) PostgreSQL and MySQL are popular open- source DBMSs that can be downloaded and used freely by anyone Microsoft’s free SQL Server Express, which is installed with Visual Studio, can be also downloaded separately from Microsoft (www.microsoft.com/express/sql)www.microsoft.com/express/sql 4

5 Relational Database A relational database organizes data simply in tables – rows (also called records) – columns (also called fields, attributes) Primary key: a column (or group of columns) requiring a unique value that cannot be duplicated in other rows A primary key composed of two or more columns is known as a composite key Foreign key—a column in this table that matches the primary-key column in another table 5

6 Example of Table: Employees 6

7 SQL A program might select data from the table to create a query result – E.g., to retrieve the location of each department, in increasing order by Department number – SQL: SELECT DISTINCT Department, Location FROM Employees ORDER BY Department 7

8 SQL Results 8

9 Schema A database may contain one or multiple tables A database’s tables, their fields and the relationships among them are collectively known as a database schema 9

10 Entity-Relationship Model Entity-Relationship (ER) model – Entity Authors Titles – Relationship There is a one-to-many relationship between a primary key and a corresponding foreign key – E.g., one author can write many books and one book can be written by many authors Others: many-to-many or one-to-one relationship E.g., AuthorISBN 10

11 11 Example of Relational Database: Books Database Books database has four tables: Authors, Publishers, AuthorISBN and Titles Authors table has three fields: author’s unique ID number, first name and last name Publishers table has two fields: publisher’s unique ID and name AuthorISBN table has two fields: authors’ ID numbers and corresponding ISBN numbers Titles has seven fields: ISBN number, title, edition number, copyright year, publisher’s ID number, book price and filename of cover image

12 Example of ER Diagram 12 AuthorISBN AuthorID ISBN Authors AuthorID FirstName LastName Publishers PublisherID PublisherName Titles ISBN Title EditionNumber Copyright PublisherID ImageFile Price 1  1  1 

13 Authors Table 13 FieldDescription AuthorIDAuthor’s ID number in the database. In the Books database, this int field is defined as an auto-incremented field. For each new record inserted in this table, the database increments the AuthorID value, ensuring that each record has a unique AuthorID. This field is the table’s primary key. FirstNameAuthor’s first name (a string). LastNameAuthor’s last name (a string). Fig. 17.3Authors table from Books. AuthorIDFirstNameLastName 1HarveyDeitel 2PaulDeitel 3TemNieto 4KateSteinbuhler 5SeanSantry 6TedLin 7PraveenSadhu 8DavidMcPhie 9CherylYaeger 10MarinaZlatkina 11BenWiedermann 12JonathanLiperi 13JeffreyListfield Fig. 17.4 Data from the Authors table of Books.

14 Publishers Table 14 FieldDescription PublisherIDThe publisher’s ID number in the database. This auto- incremented int field is the table’s primary-key field. PublisherNameThe name of the publisher (a string). Fig. 17.5Publishers table from Books. PublisherIDPublisherName 1Prentice Hall 2Prentice Hall PTG Fig. 17.6Data from the Publishers table of Books.

15 AuthorISBN Table 15 FieldDescription AuthorIDThe author’s ID number, which allows the database to associate each book with a specific author. The integer ID number in this field must also appear in the Authors table. ISBNThe ISBN number for a book (a string). Fig. 17.7AuthorISBN table from Books. AuthorIDISBNAuthorIDISBN 1013089572510130284181 1013226119710130895601 1013089571720130895725 1013528910620132261197 1013916305020130895717 1013028419x20135289106 1013016143820139163050 101308561182013028419x 1013012507520130161438 1013899394720130856118 1013085247320130125075 1013082927720138993947 1013456955520130852473

16 Titles Table 16 FieldDescription ISBNISBN number of the book (a string). TitleTitle of the book (a string). EditionNumberEdition number of the book (a string). CopyrightCopyright year of the book (an int). PublisherIDPublisher’s ID number (an int). This value must correspond to an ID number in the Publishers table. ImageFileName of the file containing the book’s cover image (a string). PriceSuggested retail price of the book (a real number). [Note: The prices shown in this database are for example purposes only.] Fig. 17.9Titles table from Books. ISBNTitleEdition -Number Publish - erID Copy- right ImageFilePrice 0130923613Python How to Program112002 python.jpg $69.95 0130622214C# How to Program112002 cshtp.jpg $69.95 0130341517Java How to Program412002 jhtp4.jpg $69.95 0130649341The Complete Java Training Course422002 javactc4.jpg $109.95 0130895601Advanced Java 2 Platform How to Program112002 advjhtp1.jpg $69.95 0130308978Internet and World Wide Web How to Program 212002 iw3htp2.jpg $69.95 0130293636Visual Basic.NET How to Program212002 vbnet.jpg $69.95 0130895636The Complete C++ Training Course322001 cppctc3.jpg $109.95

17 17 Structured Query Language (SQL) SQL keywordDescription SELECTSelects (retrieves) fields from one or more tables. FROMSpecifies tables from which to get fields or delete records. Required in every SELECT and DELETE statement. WHERESpecifies criteria that determine the rows to be retrieved. INNER JOINJoins records from multiple tables to produce a single set of records. GROUP BYSpecifies criteria for grouping records. ORDER BYSpecifies criteria for ordering records. INSERTInserts data into a specified table. UPDATEUpdates data in a specified table. DELETEDeletes data from a specified table. Fig. 17.12SQL query keywords.

18 SQL on Books Database SELECT * FROM tableName – SELECT * FROM Authors – SELECT AuthorID, LastName FROM Authors SELECT columnName1, columnName2, … FROM tableName WHERE criteria – SELECT Title, EditionNumber, Copyright FROM Titles WHERE Copyright > '2014' 18

19 SQL on Books Database (cont'd) Operator LIKE is used for pattern matching – Wildcard character Percent (%): zero or more characters Underscore (_): a single wildcard character – SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE 'D%' – SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE '_y%' 19 Deitel Ayer

20 SQL on Books Database (cont'd) SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC – ASC – DESC – SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName DESC Other statements in SQL – table1 INNER JOIN table2 ON table1.columnName=table2.columnName – INSERT INTO tableName (columnName1, columnName2, … ) VALUES (value1, value2, …) – UPDATE tableName SET columnName1 = value1, columnName2 = value2, … WHERE criteria – DELETE FROM tableName WHERE criteria 20

21  2002 Prentice Hall. All rights reserved. Outline 21 Fig. 17.22 TitleAuthor query of Books database 1 SELECT Titles.Title, Titles.ISBN, Authors.FirstName, 2 Authors.LastName, Titles.Copyright, 3 Publishers.PublisherName 4 FROM 5 ( Publishers INNER JOIN Titles 6 ON Publishers.PublisherID = Titles.PublisherID ) 7 INNER JOIN 8 ( Authors INNER JOIN AuthorISBN 9 ON Authors.AuthorID = AuthorISBN.AuthorID ) 10 ON Titles.ISBN = AuthorISBN.ISBN 11 ORDER BY Titles.Title

22 22 Python DB-API Specification Python Database Application Programming Interface (DB-API): document that specifies common object and method names for manipulating any database Describes a Connection object that accesses the database Cursor object, created by Connection object, manipulates and retrieves data Three methods for fetching rows of a query result set – fetchone, fetchmany and fetchall

23 Pypyodbc: https://code.google.com/p/pypyodbc/ import pypyodbc pypyodbc.win_create_mdb('C:\\Python34\\Books.mdb') connection_string = 'Driver={Microsoft Access Driver (*.mdb)};DBQ= C:\\Python34\\Books.mdb' connection = pypyodbc.connect(connection_string) SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));' connection.cursor().execute(SQL).commit() 23

24 Example of Using pyodbc Package http://en.wikibooks.org/wiki/Python_Programming/Dat abase_Programming import pyodbc DBfile = '/data/MSAccess/Music_Library.mdb' conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+DBfile) #use below conn if using with Access 2007, 2010.accdb file #conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBfile) cursor = conn.cursor() SQL = 'SELECT Artist, AlbumName FROM RecordCollection ORDER BY Year;' for row in cursor.execute(SQL): # cursors are iterable print row.Artist, row.AlbumName # print row # if print row it will return tuple of all fields cursor.close() conn.close() 24

25 25 Database Query Example Presents a CGI program that performs a simple query on the Books database and displays result set in an XHTML table

26  2002 Prentice Hall. All rights reserved. Outline 26 fig17_27.py #!c:\python\python.exe # Fig. 17.27: fig17_27.py # Displays contents of the Authors table, # ordered by a specified field. import MySQLdb # for Python 2.X, MySQLdb does not support Python 3 import cgi import sys def printHeader( title ): print ("""Content-type: text/html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en"> %s """ % title) # obtain user query specifications form = cgi.FieldStorage() # get "sortBy" value if form.has_key( "sortBy" ): sortBy = form[ "sortBy" ].value else: sortBy = "firstName" # get "sortOrder" value if form.has_key( "sortOrder" ): sortOrder = form[ "sortOrder" ].value else: Contains classes and functions for manipulating MySQL databasesObtain form data Get “sort by” value for ORDER BY Get sorting order for ORDER BY

27  2002 Prentice Hall. All rights reserved. Outline 27 fig17_27.py sortOrder = "ASC" printHeader( "Authors table from Books" ) # connect to database and retrieve a cursor try: connection = MySQLdb.connect( db = "Books" ) # error connecting to database except MySQLdb.OperationalError, error: print ("Error:", error) sys.exit( 1 ) # retrieve cursor else: cursor = connection.cursor() # query all records from Authors table cursor.execute( "SELECT * FROM Authors ORDER BY %s %s" % ( sortBy, sortOrder ) ) allFields = cursor.description # get field names allRecords = cursor.fetchall() # get records # close cursor and connection cursor.close() connection.close() # output results in a table print ("""\n """) # create table header for field in allFields: print (" %s " % field[ 0 ] ) Create Connection object to manage connectionSpecify database as value of keyword dbMySQLdb.connect failure raises MySQLdb.OperationalError exceptionCreate Cursor object Execute query against database Attribute description contains information about fields Obtain all records Close Cursor objectClose Connection object Output results in table

28  2002 Prentice Hall. All rights reserved. Outline 28 fig17_27.py print (" ") # display each record as a row for author in allRecords: print (" ") for item in author: print (" %s " % item) print (" ") print (" " ) # obtain sorting method from user print (""" \n Sort By: """ ) # display sorting options for field in allFields: print ("""<input type = "radio" name = "sortBy" value = "%s" />""" % field[ 0 ] ) print (field[ 0 ]) print (" " ) print (""" \nSort Order: <input type = "radio" name = "sortOrder" value = "ASC" checked = "checked" /> Ascending <input type = "radio" name = "sortOrder" value = "DESC" /> Descending \n \n\n \n """ ) Display each record as a table rowPrint form to obtain sorting information from user

29 29


Download ppt "CSCI/CMPE 4341 Topic: Programming in Python Chapter 10: Database Application Programming Interface Xiang Lian The University of Texas – Pan American Edinburg,"

Similar presentations


Ads by Google