Python和PostgreSQL GeomFromTex

2024-10-01 17:27:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python学习者

我试图在PostgreSQL中插入几何记录。在

如果我在没有geometry列的情况下尝试查询,那么它可以正常工作,并且成功地插入了所有数据。在

cur.execute("INSERT INTO taxi (userid,carNum) SELECT '"+str(msg['UserID'])+"',"+str(msg['CarNumber']))

一旦我尝试添加几何记录,什么都不会发生!执行结束时没有错误,但没有插入到数据库中。在

^{pr2}$

我想不出我在这里遗漏了什么


Tags: 数据executepostgresql记录情况msg学习者insert
1条回答
网友
1楼 · 发布于 2024-10-01 17:27:01

您需要将数据提交到数据库。在

检查psycopg2的文档http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries

遵循这些步骤

>>> import psycopg2

# Connect to an existing database
>>> conn = psycopg2.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
>>> cur = conn.cursor()

# Execute a command: this creates a new table
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")

# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion (no more SQL injections!)
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
...      (100, "abc'def"))

# Query the database and obtain data as Python objects
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchone()
(1, 100, "abc'def")

# Make the changes to the database persistent
>>> conn.commit()

# Close communication with the database
>>> cur.close()
>>> conn.close()

相关问题 更多 >

    热门问题