python psycogp2插入postgresql帮助
我有以下代码要插入到我的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'],
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他输入了错误的代码。