psycopg2 cursor.execute CREATE TABLE issue

Mitya Sirenef msirenef at lightbird.net
Sun Jan 6 16:44:47 EST 2013


On Sun 06 Jan 2013 04:38:29 PM EST, andydtaylor at gmail.com wrote:
> Hi all,
>
> I'm trying to create a process which will create a new table and populate it.
>
> But something is preventing this from working, and I don't know enough to figure it out, despite having spent most of today reading up. The code executes with no error, yet no table is created or populated.
>
> Can anyone offer me some advice? code below.
>
> Thanks,
>
> Andy
>
> #!/usr/bin/python
> import psycopg2
> import sys
>
> def main():
>     db = psycopg2.connect(
>        host = 'localhost',
>        database = 'gisdb',
>        user = 'postgres',
>        password = 'L1ncoln0ut@'
>     )
>     cursor = db.cursor()
>     cursor.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
>     cursor.execute("INSERT INTO test (num, data) VALUES (%s, %s)",(100, "abc'def"))
>
> if __name__ == "__main__":
> 	main()


To commit a transaction, you need to do a db.commit() call.

 -m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/



More information about the Python-list mailing list