1. Home
  2. Computing & Technology
  3. Python

Inserting Data Into a PostgreSQL Database

From Al Lukaszewski, for About.com

5 of 7

Python, PostgreSQL, and the 'C' Word

Finally, after passing the data to PostgreSQL, we must commit the data to the database:

connection.commit()

Now we have constructed the basic parts of our function 'insert'. Put together, the parts look like this:

connection = psycopg.connect('dbname=Birds', 'user=robert')
mark = connection.cursor()
statement = 'INSERT INTO ' + table + ' (' + columns + ') VALUES (' + values + ')'
mark.execute(statement)
connection.commit()

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Database Programming
  5. Python and PostgreSQL - Inserting Data Into a PostgreSQL Database - Python, PostgreSQL, and the 'C' Word

©2009 About.com, a part of The New York Times Company.

All rights reserved.