首页 > 解决方案 > 我在 python 中有一个字符串变量 txt ="让我做我的工作"。我想将此文本数据发送到 postgreSQL 数据库

问题描述

txt= 'let me do my work'


new_query="INSERT INTO speech (name) VALUES{}".format(str(txt))


cur.execute(new_query)

标签: python-3.xpostgresql

解决方案


尝试:

new_query = "INSERT INTO speech (name) VALUES ('{}')".format(txt)

附带说明一下,您应该避免使用 python 格式化您的查询。

这是正确(且安全)的方法:

cur.execute("INSERT INTO speech (name) VALUES (%s)", (txt,))

推荐阅读