Python PostgreSQL语句问题psycopg2cursor.execute(Table Union)

发布于 2024-11-14 04:42:37 字数 2432 浏览 3 评论 0原文

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

... 尝试: conn = psycopg2.connect("主机 = '" + 主机名 + "' dbname='" + DBName + "' 用户='" + 用户名 + "' 密码='" + 密码 + "'") curs = conn.cursor() 除了: print "无法连接到数据库"

SQLStatement = "SELECT data_partition FROM datasets WHERE map_partition='" + MapPartitions[0] + "'" curs.execute(SQL语句) ...

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

conn = psycopg2.connect("主机 = '" + 主机名 + "' dbname='" + DBName + "' 用户='" + 用户名 + "' 密码='" + 密码 + "'") 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

<块引用> <块引用>

创建表compresseddata.FRA24_Lines_2011_03 AS SELECT * FROMcompresseddata.FRA24_Lines_2011_03_Net0 UNION SELECT * FROMcompresseddata.FRA24_Lines_2011_03_Net1 UNION SELECT * FROMcompresseddata.FRA24_Lines_2011_03_Net2 UNION SELECT * FROMcompresseddata.FRA24_Lines_2011_03_Net3 UNION SELECT * FROMcompresseddata.FRA24_Lines_2011_03_Net4 UNION 从压缩数据中选择 *.FRA24_Lines_2011_03_Net5;

更改表compresseddata.FRA24_Lines_2011_03

删除列 gid;

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

有什么想法吗?

提前感谢您的帮助

I am new in python, and using Python & PostgreSQL (9.03) (and psycopg2 to interface between the two) in Windows XP environment.
I am working on a huge spatial dataset road network dataset, and seperating the data per Country through ArcGIS Geoprocessing, and automatically store and them in a PostGIS (1.5) Database.
While when retrieving values from the database everything works as planned:

...
try:
conn = psycopg2.connect("host = '" + HostName + "' dbname='" + DBName + "' user='" + Username + "' password='" + Password + "'")
curs = conn.cursor()
except:
print "Unable to connect to the database"

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

When I am trying to pass the following Union Statement to Postgres, there is no resulting table, while if I take the printed SQL Statement and run it in as an SQL Statement and run it PostgresSQL, it creates the desired resulting table:

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()

If we print the SQL Statement, this is the resulting query:

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;

I am using variables in the to Merge different Road Network Classes, and due to different Partitions of my dataset, I need to iterate through, them, but for some reason that I cannot still understand, there is no table being produced.

Any ideas?

Thanx in advance for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀里藏娇 2024-11-21 04:42:37

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

我从未尝试过此操作,但我希望执行会对此进行抱怨。

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

我建议在代码中添加异常处理并分别执行每个SQL语句,这样你就可以得到更好地报告可能出现的问题。

THe SQL you are sending are actually 3 statements, not 1.

I never tried this but I expect execute to complain about this.

Additionally there is a semicolon missing in the ALTER TABLE statement.

I would recommend to add exception handling to your code and execute each SQL statement separately so you get better error reporting on what might go wrong.

蓝咒 2024-11-21 04:42:37

确实,彼得,情况似乎确实如此。
更具体地说,每个 SQL 语句必须分别通过:

curs.execute(SQLStatement)

并通过以下方式提交:

conn.commit()

然后所有更改都将在数据库中显示出来。

再次感谢

Indeed Peter, this seems to be the case.
More specifically Each SQL Statement must be passed separately through:

curs.execute(SQLStatement)

and them committed via:

conn.commit()

All the changes will then be apparent in the database.

Thanx again

謸气贵蔟 2024-11-21 04:42:37

正如已经提到的,单独执行每个语句并检查异常可以很好地洞察正在发生的情况。

特别是 psycopg2 会引发 psycopg2.ProgrammingError。如果错误消息没有用,您可能会更好地查找异常的 pgcode,然后进行调查。
9.1 的 PG 代码:
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

注意:游标执行语句可以在单个字符串中采用多个 sql 语句。
ex: cur.execute('create table ABBA (); create table BETA ();') 是一个完全合法的语句。
因此,不要期望cursor.execute 对仅字符串输入执行任何健全性检查!

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

As already mentioned, individually executing each statement and checking the exception can provide good insight to what is occurring.

In particular psycopg2 will raise psycopg2.ProgrammingError. If the error message is not useful, you may have better luck looking up the exception's pgcode and then investigating that.
PGCodes for 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

NOTE: A cursors execute statement CAN take multiple sql statements in a single string.
ex: cur.execute('create table ABBA (); create table BETA ();') is a perfectly legitimate statement.
For this reason, do not expect cursor.execute to perform any sanity checks on a string only input!

I'd suggest (except for special rare circumstances) to execute each statement individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文