python database interfaces
TRANSCRIPT
Database interfaces
Python database interfaces
Generic Database Interfaces and APIs
• ODBC Supporto pyodbco mxODBC
• ADO Supporto adodbapi
• JDBC Supporto Integrated in Jython
Fall 2012
Python database interfaces
• Connect to a DatabaseMake a direct connection to a database and create a cursor.
pyodbc
Fall 2012
import pyodbccnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=localhost; DATABASE=testdb; UID=user; PWD=pass')cursor = cnxn.cursor()
Python database interfaces
• Selecting Some DataExecute query then fetch results.
Fall 2012
pyodbc
cursor.execute("select user_id, user_name from users")row = cursor.fetchone()print 'name:', row[1] # access by column indexprint 'name:', row.user_name # or access by name
cursor.execute("select user_id, user_name from users")rows = cursor.fetchall()for row in rows: print row.user_id, row.user_name
Python database interfaces
• Inserting or deletingExecute query then commit changes.
Fall 2012
pyodbc
cursor.execute("insert into products(id, name) values ('pyodbc', 'awesome')")cnxn.commit()
deleted = cursor.execute("delete from products where id <> 'pyodbc'").rowcountcnxn.commit()
Python database interfaces
• General Purpose Database Systems
• Database Systems for Embedding Into Applications
Fall 2012
Interfaces for Relational Database Systems
Python database interfaces
General Purpose Database Systems
• Microsoft SQL Server• Oracle• MySQL• IBM DB2• PostgreSQL• Firebird (and Interbase)• Informix• SAP DB (also known as "MaxDB")• …
Fall 2012
Python database interfaces
• mssqlo MS SQL Server module for Python
• pymssqlo A fast MS SQL server client library for Python directly using C
API instead of ODBC.
Fall 2012
Python database interfaces Fall 2012
• cx_Oracleo Lite Oracle DB Server module for Python
• DCOracle2o Advanced Python DB API 2.0 adapter for Oracle from
Zope company.
Python database interfaces Fall 2012
• MySQLdbo The most famous library for connecting MySQL
in python.
• PyMySQLo Pure-Python MySQL client library.
Python database interfaces
MySQLdb
Fall 2012
import MySQLdbconn = mysql.connect(‘localhost’, ‘username’, ‘password’, ‘db name’)cursor = conn.cursor()cursor.execute(“Example query”)cursor.commit() #needed for insert and delete
• Benefits:o Very fast and optimized ( written in C)o Has big communityo Defense SQL injectiono very efficient
Python database interfaces Fall 2012
• Ibm_dbo Python support for IBM DB2 and IBM Informix
• PyDB2o Pure-Python DB2 interface library.
Python database interfaces Fall 2012
• Psycopgo The most popular PostgreSQL adapter for the Python
• PyGreSQLo Open-source Python module that interfaces to
a PostgreSQL database
Python database interfaces
Interfaces for Non-Relational Database Systems
• MetaKit• ZODB• Berkeley DB• Durus• Atop
Fall 2012
Python database interfaces
Native Python Databases
• Buzhugo buzhug is a fast, portable, pure-Python database engine, using
a pythonic non-SQL syntax for all operations.
• SnakeSQLo SnakeSQL is a pure Python SQL database written to
remove the dependence of the Python Web Modules on 3rd party drivers for non-Python databases.
Fall 2012
Python database interfaces Fall 2012
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic
design.
Python database interfaces
Set database server
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.’ # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'main_db.db', # Or path to database file if using sqlite3. 'USER': '', 'PASSWORD': '', 'HOST': '', # Set to empty string for localhost. 'PORT': '', # Set to empty string for default. }}
Fall 2012
Python database interfaces
Using multiple database
• DATABASES = { 'default': {
'NAME': 'app_data', 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'USER': 'postgres_user', 'PASSWORD': 's3krit' },
'users': { 'NAME': 'user_data',
'ENGINE': 'django.db.backends.mysql', 'USER': 'mysql_user', 'PASSWORD': 'priv4te' } }
Fall 2012
Python database interfaces
Resources
Fall 2012
• Expert Python Programming
Python database interfaces
Resources
Fall 2012
• wiki.python.org
• www.djangoproject.com
Python database interfaces
THANK YOU
Fall 2012