python database interfaces

21
Database interfaces beheshtraya@gmail. com

Upload: seyed-mohammad-javad-beheshtian

Post on 13-May-2015

3.335 views

Category:

Education


8 download

TRANSCRIPT

Page 1: Python database  interfaces

Database interfaces

[email protected]

Page 2: Python database  interfaces

Python database interfaces

Generic Database Interfaces and APIs

• ODBC Supporto pyodbco mxODBC

• ADO Supporto adodbapi

• JDBC Supporto Integrated in Jython

Fall 2012

Page 3: Python database  interfaces

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

Page 4: Python database  interfaces

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

Page 5: Python database  interfaces

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

Page 6: Python database  interfaces

Python database interfaces

• General Purpose Database Systems

• Database Systems for Embedding Into Applications

Fall 2012

Interfaces for Relational Database Systems

Page 7: Python database  interfaces

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

Page 8: Python database  interfaces

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

Page 9: Python database  interfaces

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.

Page 10: Python database  interfaces

Python database interfaces Fall 2012

• MySQLdbo The most famous library for connecting MySQL

in python.

• PyMySQLo  Pure-Python MySQL client library.

Page 11: Python database  interfaces

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

Page 12: Python database  interfaces

Python database interfaces Fall 2012

• Ibm_dbo Python support for IBM DB2 and IBM Informix

• PyDB2o  Pure-Python DB2 interface library.

Page 13: Python database  interfaces

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

Page 14: Python database  interfaces

Python database interfaces

Interfaces for Non-Relational Database Systems

• MetaKit• ZODB• Berkeley DB• Durus• Atop

Fall 2012

Page 15: Python database  interfaces

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

Page 16: Python database  interfaces

Python database interfaces Fall 2012

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic

design.

Page 17: Python database  interfaces

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

Page 18: Python database  interfaces

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

Page 19: Python database  interfaces

Python database interfaces

Resources

Fall 2012

• Expert Python Programming

Page 20: Python database  interfaces

Python database interfaces

Resources

Fall 2012

• wiki.python.org

• www.djangoproject.com

Page 21: Python database  interfaces

Python database interfaces

THANK YOU

Fall 2012