SlideShare a Scribd company logo
Database interfaces
  beheshtraya@gmail.com
Generic Database Interfaces
         and APIs
• ODBC Support
   o pyodbc
   o mxODBC


• ADO Support
   o adodbapi


• JDBC Support
   o Integrated in Jython




Python database interfaces   Fall 2012
pyodbc
• Connect to a Database
     Make a direct connection to a database and
     create a cursor.


   import pyodbc
   cnxn = pyodbc.connect('DRIVER={SQL Server};
                         SERVER=localhost;
                         DATABASE=testdb;
                         UID=user;
                         PWD=pass')
   cursor = cnxn.cursor()



Python database interfaces                       Fall 2012
pyodbc
• Selecting Some Data
      Execute query then fetch results.

cursor.execute("select user_id, user_name from users")
row = cursor.fetchone()
print 'name:', row[1]        # access by column index
print '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                              Fall 2012
pyodbc
• Inserting or deleting
      Execute query then commit changes.


cursor.execute("insert into products(id, name) values ('pyodbc',
'awesome')")
cnxn.commit()



deleted = cursor.execute("delete from products where id <> 'pyodbc'").r
cnxn.commit()




  Python database interfaces                                 Fall 2012
Interfaces for Relational
             Database Systems

• General Purpose Database Systems

• Database Systems for Embedding Into
  Applications




Python database interfaces              Fall 2012
General Purpose Database
                  Systems
•     Microsoft SQL Server
•     Oracle
•     MySQL
•     IBM DB2
•     PostgreSQL
•     Firebird (and Interbase)
•     Informix
•     SAP DB (also known as "MaxDB")
•     …

    Python database interfaces         Fall 2012
• mssql
   o MS SQL Server module for Python




• pymssql
   o A fast MS SQL server client library for Python directly using C API
     instead of ODBC.




Python database interfaces                                         Fall 2012
• cx_Oracle
   o Lite Oracle DB Server module for Python




• DCOracle2
   o    Advanced Python DB API 2.0 adapter for Oracle from
       Zope company.




Python database interfaces                                   Fall 2012
• MySQLdb
   o The most famous library for connecting MySQL
     in python.



• PyMySQL
   o    Pure-Python MySQL client library.




Python database interfaces                          Fall 2012
MySQLdb
• Benefits:
   o    Very fast and optimized ( written in C)
   o    Has big community
   o    Defense SQL injection
   o    very efficient



   import MySQLdb
   conn = mysql.connect(‘localhost’, ‘username’, ‘password’, ‘db name’)
   cursor = conn.cursor()
   cursor.execute(‚Example query‛)
   cursor.commit()    #needed for insert and delete




 Python database interfaces                                        Fall 2012
• Ibm_db
   o Python support for IBM DB2 and IBM Informix



• PyDB2
   o    Pure-Python DB2 interface library.




Python database interfaces                         Fall 2012
• Psycopg
   o The most popular PostgreSQL adapter for the Python




• PyGreSQL
   o Open-source Python module that interfaces to
     a PostgreSQL database




Python database interfaces                                Fall 2012
Interfaces for Non-Relational
            Database Systems

•     MetaKit
•     ZODB
•     Berkeley DB
•     Durus
•     Atop




    Python database interfaces   Fall 2012
Native Python Databases
• Buzhug
   o buzhug is a fast, portable, pure-Python database engine, using a
     pythonic non-SQL syntax for all operations.



• SnakeSQL
   o 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.




Python database interfaces                                    Fall 2012
Django is a high-level Python Web
    framework that encourages rapid
   development and clean, pragmatic
                  design.



Python database interfaces         Fall 2012
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.
  }
}




 Python database interfaces                                                   Fall 2012
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' }
}




    Python database interfaces                                      Fall 2012
Resources

• Expert Python
  Programming




Python database interfaces               Fall 2012
Resources

• wiki.python.org



• www.djangoproject.com




Python database interfaces               Fall 2012
Python database interfaces   Fall 2012

More Related Content

PPTX
OOPS In JAVA.pptx
Sachin33417
 
PPTX
python conditional statement.pptx
Dolchandra
 
PPT
Linux directory commands:more options on cd and ls command
bhatvijetha
 
PPTX
Presentation on python
william john
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
PDF
Python - the basics
University of Technology
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PDF
Files in java
Muthukumaran Subramanian
 
OOPS In JAVA.pptx
Sachin33417
 
python conditional statement.pptx
Dolchandra
 
Linux directory commands:more options on cd and ls command
bhatvijetha
 
Presentation on python
william john
 
Java Streams
M Vishnuvardhan Reddy
 
Python - the basics
University of Technology
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 

What's hot (20)

PPTX
Inheritance in OOPs with java
AAKANKSHA JAIN
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
Inheritance ppt
Nivegeetha
 
PPTX
Conditionalstatement
RaginiJain21
 
PDF
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
PPTX
C++ decision making
Zohaib Ahmed
 
PPT
Python Programming Language
Dr.YNM
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Python programming
Ashwin Kumar Ramasamy
 
PPTX
Modules in Python Programming
sambitmandal
 
PDF
Python GUI
LusciousLarryDas
 
PDF
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
PPTX
Inheritance in java
Tech_MX
 
PPT
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Introduction to java
Veerabadra Badra
 
PPT
Python ppt
Mohita Pandey
 
PPTX
Operators and expressions
vishaljot_kaur
 
PPTX
JAVA ENVIRONMENT
josemachoco
 
Inheritance in OOPs with java
AAKANKSHA JAIN
 
Python Flow Control
Mohammed Sikander
 
Java Data Types
Spotle.ai
 
Inheritance ppt
Nivegeetha
 
Conditionalstatement
RaginiJain21
 
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
C++ decision making
Zohaib Ahmed
 
Python Programming Language
Dr.YNM
 
Modules and packages in python
TMARAGATHAM
 
Python programming
Ashwin Kumar Ramasamy
 
Modules in Python Programming
sambitmandal
 
Python GUI
LusciousLarryDas
 
PHP Loops and PHP Forms
M.Zalmai Rahmani
 
Inheritance in java
Tech_MX
 
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Control Statements in Java
Niloy Saha
 
Introduction to java
Veerabadra Badra
 
Python ppt
Mohita Pandey
 
Operators and expressions
vishaljot_kaur
 
JAVA ENVIRONMENT
josemachoco
 
Ad

Viewers also liked (20)

PPTX
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Relational Database Access with Python
Mark Rees
 
PDF
Information is Everything: Marketing in the Age of Disruption
Tahzoo
 
PPTX
Untitled presentation (1)
Simo Lensment
 
PPTX
Seventies Swagger
LIM College
 
PDF
Educamers - education and gamers
CROWDERRY
 
PPT
Fashion of China Andrew Charette
LIM College
 
PPT
Understanding social media
Sara Carbaugh
 
PDF
IED - Progettazione 3 - SoyJoy
Francesco Cardarelli
 
PPT
Our thought is our life
kmbehzad
 
PDF
Prosumerism
CROWDERRY
 
PDF
LEVELAPPME Summer School Report 2012
CROWDERRY
 
PDF
IED - Progettazione 4 - AgosDucati, un mondo unico
Francesco Cardarelli
 
PPTX
Colors are all around us
Chaasadyah
 
PPTX
China presentation
tdematties
 
PPTX
Colors are all around us
Chaasadyah
 
PPTX
Cipa nº 5
pedagogia2012melgar
 
PDF
Camp tech
npolk
 
PPT
Dutch Spices UK
dutchspices
 
PDF
Qr qrazy
npolk
 
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Relational Database Access with Python
Mark Rees
 
Information is Everything: Marketing in the Age of Disruption
Tahzoo
 
Untitled presentation (1)
Simo Lensment
 
Seventies Swagger
LIM College
 
Educamers - education and gamers
CROWDERRY
 
Fashion of China Andrew Charette
LIM College
 
Understanding social media
Sara Carbaugh
 
IED - Progettazione 3 - SoyJoy
Francesco Cardarelli
 
Our thought is our life
kmbehzad
 
Prosumerism
CROWDERRY
 
LEVELAPPME Summer School Report 2012
CROWDERRY
 
IED - Progettazione 4 - AgosDucati, un mondo unico
Francesco Cardarelli
 
Colors are all around us
Chaasadyah
 
China presentation
tdematties
 
Colors are all around us
Chaasadyah
 
Camp tech
npolk
 
Dutch Spices UK
dutchspices
 
Qr qrazy
npolk
 
Ad

Similar to Python database interfaces (20)

PPTX
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
PDF
Programming with Python and PostgreSQL
Peter Eisentraut
 
PPTX
Chapter 6 Interface Python with MYSQL.pptx
sarofba
 
PPTX
Class 12 CS Ch-16 MySQL PPT.pptx
DeepaG66
 
PDF
Scripts Python Dbapi
AkramWaseem
 
PDF
MySQL Tech Café #8: MySQL 8.0 for Python Developers
Frederic Descamps
 
PPTX
unit-5 SQL 1 creating a databse connection.pptx
HuzaifaAhmedFarooqi
 
PDF
Interface python with sql database10.pdf
HiteshNandi
 
PPTX
PythonDatabaseAPI -Presentation for Database
dharawagh9999
 
PDF
AmI 2015 - Databases in Python
Fulvio Corno
 
PPTX
DATABASE CONNECTIVITY PYTHON USING MYSQL/SQLITE/POSTGRE
deepalishinkar1
 
PDF
Interface python with sql database.pdf--
jagaspeed09
 
PDF
Interface python with sql database.pdf
MohammadImran709594
 
PPTX
Database Connectivity using Python and MySQL
devsuchaye
 
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
PDF
Www Kitebird Com Articles Pydbapi Html Toc 1
AkramWaseem
 
PDF
Develop Python Applications with MySQL Connector/Python
Jesper Wisborg Krogh
 
PPTX
Chapter -7.pptx
MikialeTesfamariam
 
PPTX
python db connection samples and program
usha raj
 
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
Programming with Python and PostgreSQL
Peter Eisentraut
 
Chapter 6 Interface Python with MYSQL.pptx
sarofba
 
Class 12 CS Ch-16 MySQL PPT.pptx
DeepaG66
 
Scripts Python Dbapi
AkramWaseem
 
MySQL Tech Café #8: MySQL 8.0 for Python Developers
Frederic Descamps
 
unit-5 SQL 1 creating a databse connection.pptx
HuzaifaAhmedFarooqi
 
Interface python with sql database10.pdf
HiteshNandi
 
PythonDatabaseAPI -Presentation for Database
dharawagh9999
 
AmI 2015 - Databases in Python
Fulvio Corno
 
DATABASE CONNECTIVITY PYTHON USING MYSQL/SQLITE/POSTGRE
deepalishinkar1
 
Interface python with sql database.pdf--
jagaspeed09
 
Interface python with sql database.pdf
MohammadImran709594
 
Database Connectivity using Python and MySQL
devsuchaye
 
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
Www Kitebird Com Articles Pydbapi Html Toc 1
AkramWaseem
 
Develop Python Applications with MySQL Connector/Python
Jesper Wisborg Krogh
 
Chapter -7.pptx
MikialeTesfamariam
 
python db connection samples and program
usha raj
 

Recently uploaded (20)

PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
High Ground Student Revision Booklet Preview
jpinnuck
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
The Final Stretch: How to Release a Game and Not Die in the Process.
Marta Fijak
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
High Ground Student Revision Booklet Preview
jpinnuck
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 

Python database interfaces

  • 2. Generic Database Interfaces and APIs • ODBC Support o pyodbc o mxODBC • ADO Support o adodbapi • JDBC Support o Integrated in Jython Python database interfaces Fall 2012
  • 3. pyodbc • Connect to a Database Make a direct connection to a database and create a cursor. import pyodbc cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=localhost; DATABASE=testdb; UID=user; PWD=pass') cursor = cnxn.cursor() Python database interfaces Fall 2012
  • 4. pyodbc • Selecting Some Data Execute query then fetch results. cursor.execute("select user_id, user_name from users") row = cursor.fetchone() print 'name:', row[1] # access by column index print '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 Fall 2012
  • 5. pyodbc • Inserting or deleting Execute query then commit changes. cursor.execute("insert into products(id, name) values ('pyodbc', 'awesome')") cnxn.commit() deleted = cursor.execute("delete from products where id <> 'pyodbc'").r cnxn.commit() Python database interfaces Fall 2012
  • 6. Interfaces for Relational Database Systems • General Purpose Database Systems • Database Systems for Embedding Into Applications Python database interfaces Fall 2012
  • 7. General Purpose Database Systems • Microsoft SQL Server • Oracle • MySQL • IBM DB2 • PostgreSQL • Firebird (and Interbase) • Informix • SAP DB (also known as "MaxDB") • … Python database interfaces Fall 2012
  • 8. • mssql o MS SQL Server module for Python • pymssql o A fast MS SQL server client library for Python directly using C API instead of ODBC. Python database interfaces Fall 2012
  • 9. • cx_Oracle o Lite Oracle DB Server module for Python • DCOracle2 o Advanced Python DB API 2.0 adapter for Oracle from Zope company. Python database interfaces Fall 2012
  • 10. • MySQLdb o The most famous library for connecting MySQL in python. • PyMySQL o Pure-Python MySQL client library. Python database interfaces Fall 2012
  • 11. MySQLdb • Benefits: o Very fast and optimized ( written in C) o Has big community o Defense SQL injection o very efficient import MySQLdb conn = mysql.connect(‘localhost’, ‘username’, ‘password’, ‘db name’) cursor = conn.cursor() cursor.execute(‚Example query‛) cursor.commit() #needed for insert and delete Python database interfaces Fall 2012
  • 12. • Ibm_db o Python support for IBM DB2 and IBM Informix • PyDB2 o Pure-Python DB2 interface library. Python database interfaces Fall 2012
  • 13. • Psycopg o The most popular PostgreSQL adapter for the Python • PyGreSQL o Open-source Python module that interfaces to a PostgreSQL database Python database interfaces Fall 2012
  • 14. Interfaces for Non-Relational Database Systems • MetaKit • ZODB • Berkeley DB • Durus • Atop Python database interfaces Fall 2012
  • 15. Native Python Databases • Buzhug o buzhug is a fast, portable, pure-Python database engine, using a pythonic non-SQL syntax for all operations. • SnakeSQL o 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. Python database interfaces Fall 2012
  • 16. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Python database interfaces Fall 2012
  • 17. 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. } } Python database interfaces Fall 2012
  • 18. 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' } } Python database interfaces Fall 2012
  • 19. Resources • Expert Python Programming Python database interfaces Fall 2012