python - psycopg2 cannot execute multiple queries -
i'm having issues executing multiple queries on psql db using psycopg2. example:
#!/usr/bin/env python #-*- coding: utf-8 -*- import psycopg2 psycopg2.extras import realdictcursor def createuser(user, myschema): conn = psycopg2.connect("dbname='postgres' user='root' password='somepassword' host='localhost'") cur = conn.cursor() cur.execute("""create user %s""" % (user)) conn.commit() cur.close() conn.close() createschema(user, myschema) def createschema(user, myschema): conn = psycopg2.connect("dbname='postgres' user='root' password='somepassword' host='localhost'") cur = conn.cursor() cur.execute("""create schema %s authorization %s """ % (user,myschema)) conn.commit() cur.close() conn.close() def fetchuserinput(): userinput = raw_input("username") myschema = raw_input("schemaname") createuser(userinput, myschema) fetchuserinput()
in case, second query fails error user created not exist! if execute createuser function, works fine. if execute manually within psql, works fine.
as if first commit isnt executed on database when open second connection within createschema function, makes no sense.
what doing wrong?
looks you've inverted 2 parameters in second query :
cur.execute("""create schema %s authorization %s """ % (myschema, user))
some doc:
create schema schema_name [ authorization user_name ] [ schema_element [ ... ] ]
create schema authorization user_name [ schema_element [ ... ] ]
create schema if not exists schema_name [ authorization user_name ]
create schema if not exists authorization user_name
Comments
Post a Comment