【问题标题】:Unable to Create Schema with pscopg2 library in Python无法在 Python 中使用 pscopg2 库创建架构
【发布时间】:2017-07-17 03:41:25
【问题描述】:

在我的 python 脚本中,我尝试使用以下代码在我的 Postgresql 数据库中创建一个新架构:

city = "New York"

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

运行此代码时,程序会抛出以下错误:

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

由于某种原因,名称已插入引号,导致程序无法创建架构。在做了一些研究之后,我确定我在这里使用的语法是正确的,并且我尝试使用其他适用于 psycopg2 的语法并且仍然收到相同的错误。

【问题讨论】:

    标签: python sql postgresql psycopg2


    【解决方案1】:

    试试AsIs,如下:

    import psycopg2
    from psycopg2.extensions import AsIs
    
    city = AsIs("New_York")
    
    cur.execute("CREATE SCHEMA %s", (city,)) # Creates New Schema
    

    这里有一个similar question 也使用了这个函数。希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      甚至比AsIs 更好,使用psycopg2.sql

      【讨论】:

        猜你喜欢
        • 2021-02-10
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多