gpt4 book ai didi

Python 和 PostgreSQL - GeomFromText

转载 作者:行者123 更新时间:2023-11-29 12:20:36 25 4
gpt4 key购买 nike

我是一名 Python 学习者,

我正在尝试将几何记录插入 PostgreSQL。

如果我在没有几何列的情况下尝试查询,它工作正常并且所有数据都成功插入。

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

当我尝试添加几何记录时,没有任何反应!执行结束时没有错误,但没有任何内容被插入到数据库中。

cur.execute("INSERT INTO taxi (position,userid,carNum) SELECT GeomFromText('POINT("+str(float(msg['longitude']))+" "+str(float(msg['latitude']))+")',4326),'"+str(msg['UserID'])+"',"+str(msg['CarNumber']))

无法弄清楚我在这里遗漏了什么

最佳答案

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

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

关于Python 和 PostgreSQL - GeomFromText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com