Python PostgreSQL语句问题psycopg2 cursor.execute(表联盟)

我是python中的新手,在Windows XP环境中使用Python和PostgreSQL(9.03)(以及两者之间的psycopg2接口) . 我正在研究一个巨大的空间数据集道路网络数据集,并通过ArcGIS地理处理将每个国家的数据分开,并自动将它们存储在PostGIS(1.5)数据库中 . 从数据库中检索值时,一切都按计划运行:

... try:conn = psycopg2.connect(“host ='”HostName“'dbname ='”DBName“'user ='”Username“'password ='”Password“'”)curs = conn.cursor()除外:print“无法连接到数据库”

SQLStatement =“SELECT data_partition FROM datasets WHERE map_partition ='”MapPartitions [0]“'”curs.execute(SQLStatement)...

当我尝试将以下Union语句传递给Postgres时,没有结果表,而如果我将打印的SQL语句作为SQL语句运行并运行它PostgresSQL,它会创建所需的结果表:

conn = psycopg2.connect(“host ='”HostName“'dbname ='”DBName“'user ='”Username“'password ='”Password“'”)cur = conn.cursor()

SQLStatement =(

"CREATE TABLE " + Schema + "." + PartitionTableName + " AS \n"
            "SELECT * FROM " + Schema + "." + partName + "_Lines_" + Rel + "_Net0 UNION \n"
            "SELECT * FROM " + Schema + "." + partName + "_Lines_" + Rel + "_Net1 UNION \n"
            "SELECT * FROM " + Schema + "." + partName + "_Lines_" + Rel + "_Net2 UNION \n"
            "SELECT * FROM " + Schema + "." + partName + "_Lines_" + Rel + "_Net3 UNION \n"
            "SELECT * FROM " + Schema + "." + partName + "_Lines_" + Rel + "_Net4 UNION \n"
            "SELECT * FROM " + Schema + "." + partName + "_Lines_" + Rel + "_Net5;\n"
            "\n"
            "\n"
            "ALTER TABLE " + Schema + "." + partName + "_Lines_" + Rel + "\n"
            "DROP COLUMN gid;\n"

cur.execute(的SQLStatement)

conn.commit()

        cur.close()

如果我们打印SQL语句,这是结果查询:

print SQLStatement

CREATE TABLE compresseddata.FRA24_Lines_2011_03 AS SELECT * FROM compresseddata.FRA24_Lines_2011_03_Net0 UNION SELECT * FROM compresseddata.FRA24_Lines_2011_03_Net1 UNION SELECT * FROM compresseddata.FRA24_Lines_2011_03_Net2 UNION SELECT * FROM compresseddata.FRA24_Lines_2011_03_Net3 UNION SELECT * FROM compresseddata.FRA24_Lines_2011_03_Net4 UNION SELECT * FROM compresseddata.FRA24_Lines_2011_03_Net5; ALTER TABLE compresseddata.FRA24_Lines_2011_03 DROP COLUMN gid;

我在合并不同的道路网络类中使用变量,并且由于我的数据集的不同分区,我需要迭代它们,但由于某些原因我无法理解,没有生成表格 .

有任何想法吗?

提前帮助Thanx

回答(3)

2 years ago

您发送的SQL实际上是3个语句,而不是1个 .

我从来没有试过这个,但我希望执行抱怨这个 .

此外,ALTER TABLE语句中缺少分号 .

我建议添加exception handling to your code并单独执行每个SQL语句,以便更好地报告可能出错的错误 .

2 years ago

事实上,彼得,情况似乎如此 . 更具体地说,每个SQL语句必须单独传递:

curs.execute(SQLStatement)

他们承诺通过:

conn.commit()

然后,所有更改都将在数据库中显现出来 .

再次谢谢

2 years ago

如前所述,单独执行每个语句并检查异常可以提供对正在发生的事情的良好洞察 .

特别是psycopg2会引发psycopg2.ProgrammingError . 如果错误消息没用,您可能会更好地查找异常的pgcode,然后再进行调查 . PGC编码为9.1:http://www.postgresql.org/docs/9.1/static/errcodes-appendix.html) .

try:
    cur.execute(SQLQUERY)
except psycopg2.ProgrammingError as e:
    # Err code lookup at http://www.postgresql.org/docs/9.1/static/errcodes-appendix.html
    print "psycopg2 error code %s" % e.pgcode
    raise e

注意:游标执行语句CAN可以在单个字符串中使用多个sql语句 . 例如: cur.execute('create table ABBA (); create table BETA ();') 是完全合法的陈述 . 出于这个原因,不要指望cursor.execute只对字符串输入执行任何健全性检查!

我建议(除了特殊的罕见情况)单独执行每个语句 .