0
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
Python database connection:
Shameena v
shameenavayoli@gmail.com
www.facebook.com/shamee
navayoli
twitter.com/username
in.l...
Introduction
• Psycopg2 is the most popular PostgreSql adapter
for the Python programming language
• core it fully impleme...
Steps:
1) We want to import psycopg2 in code
Import psycopg2
2) Connect to an existing database
conn = psycopg2.connect("d...
• Query the database and obtain data as Python
objects
cur.execute("SELECT * FROM test;")
cur.fetchone() (1, 100, "abc'def...
• The function connect() creates a new database
session and returns a new connection instance.
• The class connection enca...
.
• The class cursor allows interaction with the
database:
---send commands to the database using methods
such as execute(...
Example:
import psycopg2
try:
con=psycopg2.connect("dbname = testdb user = shafeequemonp'")
cur=con.cursor()
cur.execute('...
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.co...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
Python database connection
Upcoming SlideShare
Loading in...5
×

Python database connection

1,187

Published on

Python database connection

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,187
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Python database connection"

  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. Python database connection: Shameena v shameenavayoli@gmail.com www.facebook.com/shamee navayoli twitter.com/username in.linkedin.com/in/shamean 9567135569
  3. 3. Introduction • Psycopg2 is the most popular PostgreSql adapter for the Python programming language • core it fully implements the pythondb API 2.0 • Install the Psycopg2 Command for installation: • sudo apt-get install python-psycopg2 Creating user Here we install the psycopg2 module on a Ubuntu system. $ sudo -u postgres createuser janbodnar Shall the new role be a superuser? (y/n) y
  4. 4. Steps: 1) We want to import psycopg2 in code Import psycopg2 2) Connect to an existing database conn = psycopg2.connect("dbname=test user=postgres") cur=conn.cursor() 3) Execute a command: this creates a new table cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
  5. 5. • Query the database and obtain data as Python objects cur.execute("SELECT * FROM test;") cur.fetchone() (1, 100, "abc'def") 4) Make the changes to the database persistent conn.commit() 5) Close communication with the database conn.close()
  6. 6. • The function connect() creates a new database session and returns a new connection instance. • The class connection encapsulates a database session. It allows to: create new cursor using the cursor()method to execute database commands and queries, terminate transactions using the methods commit()or rollback()
  7. 7. . • The class cursor allows interaction with the database: ---send commands to the database using methods such as execute() and executemany() ---retrieve data from the database by iteration or using methods such as fetchone(), fetchmany(), fetchall().
  8. 8. Example: import psycopg2 try: con=psycopg2.connect("dbname = testdb user = shafeequemonp'") cur=con.cursor() cur.execute('SELECT version()') ver=cur.fetchone() print ver except psycopg2.DatabaseError.e: print 'Error %s' % e sys.exit(1) finally: if con: con.close()
  9. 9. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  10. 10. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×