Sie sind auf Seite 1von 14

Advanced SQL

Mayank Singh

CSE, IITGN

February 7, 2020

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 1 / 14


Outline

How to access SQL from a general-purpose programming language


How extend the SQL language to support procedural actions
How to allow functions defined in procedural languages to be executed
within the database
Triggers, recursive queries and advanced aggregation features

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 2 / 14


Accessing SQL From a Programming Language

Not all queries can be expressed in SQL


No support for printing a report, interacting with a user, or sending the
results of a query to a graphical user interface, etc.

Two approaches
Dynamic SQL: A general-purpose program can communicate with a
database using a collection of functions at runtime.
Embedded SQL: SQL statements are identified at compile time using a
preprocessor which submits the statements to the database system for
precompilation and optimization.

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 3 / 14


Python DB-API

The Python standard for database interfaces. It supports:


GadFly
mSQL
MySQL
PostgreSQL
Microsoft SQL Server 2000
Informix
Interbase
Oracle
Sybase
http://wiki.python.org/moin/DatabaseInterfaces

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 4 / 14


MySQLdb

Interface for connecting to a MySQL database server from Python.

Importing module
#!/usr/bin/python
import MySQLdb

Installing module
$ sudo apt-get install python-dev libmysqlclient-dev [Linux]
or
pip install MySQL-python [Python command prompt]

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 5 / 14


MySQLdb

Connecting Database

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 6 / 14


MySQLdb

Creating Database Table

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 7 / 14


MySQLdb
Insert Operation

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 8 / 14


MySQLdb

Read Operations
fetchone(): fetches the next row of a query result set
fetchall(): fetches all the rows in a result set
rowcount(): returns the number of rows that were affected by an
execute() method

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 9 / 14


MySQLdb

Fetchall()

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 10 / 14


More Resources

A great tutorial
https://www.tutorialspoint.com/python/python_database_access.htm

A detailed introduction
http://structure.usc.edu/mysqldb/MySQLdb-3.html

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 11 / 14


Embedded SQL

A general template

An example

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 12 / 14


Embedded SQL

Declaring a cursor

The open statement

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 13 / 14


Embedded SQL

Fetching tuples

Delete the temporary relation

Mayank Singh (IIT Gandhinagar) Advanced SQL February 7, 2020 14 / 14

Das könnte Ihnen auch gefallen