1. Home
  2. Computing & Technology
  3. Python

Inserting Data Into a PostgreSQL Database

By Al Lukaszewski, 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()

5 of 7

Explore Python

More from About.com

  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

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

All rights reserved.