leopardsticks
leopardsticks

Reputation: 127

Unable to Create Schema with pscopg2 library in Python

In my python script I am attempting to create a new schema inside my Postgresql database using the following code:

city = "New York"

cur.execute("CREATE SCHEMA %s", (city,)) # Creates New Schema

When this code is run the program throws the following error:

psycopg2.ProgrammingError: syntax error at or near "'New York'"
LINE 1: CREATE SCHEMA 'New York' code here

For some reason the name has been inserted into quotes which causes the program to be unable to create the schema. After doing some research I am certain that the syntax I've used here is correct and I have tried using other syntax that works with psycopg2 and still receive the same error.

Upvotes: 0

Views: 550

Answers (2)

piro
piro

Reputation: 13931

Even better than AsIs, use psycopg2.sql.

Upvotes: 0

teddyterminal
teddyterminal

Reputation: 36

Try AsIs, as follows:

import psycopg2
from psycopg2.extensions import AsIs

city = AsIs("New_York")

cur.execute("CREATE SCHEMA %s", (city,)) # Creates New Schema

Here's a similar question that also used this function. Hope this helps!

Upvotes: 2

Related Questions