Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python psycogp2插入postgresql帮助_Python_Sql_Postgresql_Psycopg2 - Fatal编程技术网

python psycogp2插入postgresql帮助

python psycogp2插入postgresql帮助,python,sql,postgresql,psycopg2,Python,Sql,Postgresql,Psycopg2,我有以下代码要插入到我的postgresql数据库中 conn = psycopg2.connect("my connection setting are in here") cur = conn.cursor() cur.execute('INSERT INTO src_event (location_id, catname, title, name) VALUES (%i, \"%s\", \"%s\", \"%s\")' % (1441, "concert", item['title'],

我有以下代码要插入到我的postgresql数据库中

conn = psycopg2.connect("my connection setting are in here")
cur = conn.cursor()
cur.execute('INSERT INTO src_event (location_id, catname, title, name) VALUES (%i, \"%s\", \"%s\", \"%s\")' % (1441, "concert", item['title'], item['artists'] )) 
但是,当我运行此命令时,会出现以下错误:

psycopg2.ProgrammingError: column "concert" does not exist
LINE 1: ...(location_id, catname, title, name) VALUES (1441, concert, "...
但是“concert”不是一个列,而是一个值,所以我不明白为什么会出现这个错误

编辑-我尝试过在价值音乐会上放\“四舍五入”,但没有


我如何才能在不出现此错误的情况下插入数据?

您真的不应该使用python字符串格式来构建查询-它们很容易出错。而您的实际问题是,您使用“for quoting,而您必须使用”来引用(“quotes table/column names等,'quotes string”)

请改用以下代码:

cur.execute('INSERT INTO src_event (location_id, catname, title, name) VALUES (%s, %s, %s, %s)', (1441, 'concert', item['title'], item['artists']))
请注意,无论实际使用何种类型,都必须使用
%s


另请参见。

谢谢,这样就消除了该错误,但现在我得到了异常。TypeError:int参数是必需的。唯一应该是int的字段是catname,它是int。您必须对所有值使用
%s
,即使是int值。我在开始时有错误,然后编辑了我的答案。您可能复制了t他输入了错误的代码。