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

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
 2002 Prentice Hall. All rights reserved. 1 Chapter 17 – Customizing Classes Outline 17.1 Introduction 17.2 Relational Database Model 17.3 Relational.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
3-1 Chapter 3 Data and Knowledge Management
1 Introduction to Web Application Introduction to Data Base.
Introduction to Structured Query Language (SQL)
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Attribute databases. GIS Definition Diagram Output Query Results.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Exercise SELECT authorID, lastName FROM authors AuthorID FirstName
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational.
ASP.NET Programming with C# and SQL Server First Edition
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Chapter 1 Overview of Database Concepts Oracle 10g: SQL
Python MySQL Database Access
Lecture 2 An Overview of Relational Database IST 318 – DB Admin.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Database: SQL and MySQL
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
CSCI/CMPE 4341 Topic: Programming in Python Review: Final Exam Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Introduction to Database Programming with Python Gary Stewart
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Database, SQL and ADO.NET
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Visual Basic 2010 How to Program
Chapter 5 Introduction to SQL.
Database Programming in Java
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Databases Intro (from Deitel)
The University of Texas – Pan American
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
Chapter 22 - SQL, MySQL, DBI and ADO
Structured Query Language
Introduction To Structured Query Language (SQL)
Presentation transcript:

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

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

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

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 ( 4

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

Example of Table: Employees 6

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

SQL Results 8

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

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 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

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 

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 Data from the Authors table of Books.

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.

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 x x

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 Python How to Program python.jpg $ C# How to Program cshtp.jpg $ Java How to Program jhtp4.jpg $ The Complete Java Training Course javactc4.jpg $ Advanced Java 2 Platform How to Program advjhtp1.jpg $ Internet and World Wide Web How to Program iw3htp2.jpg $ Visual Basic.NET How to Program vbnet.jpg $ The Complete C++ Training Course cppctc3.jpg $109.95

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 SQL query keywords.

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

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

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

 2002 Prentice Hall. All rights reserved. Outline 21 Fig 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 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

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

Example of Using pyodbc Package 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 Database Query Example Presents a CGI program that performs a simple query on the Books database and displays result set in an XHTML table

 2002 Prentice Hall. All rights reserved. Outline 26 fig17_27.py #!c:\python\python.exe # Fig : 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 = " 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

 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

 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