问题描述
我一直得到这个
错误表明 someentry someentry
不存在c $ c>不是列,它只是要输入数据库的值。
The error indicates that the column someentry
does not exist when someentry
is not a column it's just a value to enter into the db.
以下是给出错误的代码:
Here is the code that gives the error:
cur.execute('INSERT INTO {0!s} (ip_id, item) VALUES ({1!s}{2!s})'.format('mytable',1,'someentry'))
这是我创建表格的方式:
Here is how I create my table:
tablename = 'mytable'
command = """
CREATE TABLE IF NOT EXISTS {} (
ip_id SERIAL PRIMARY KEY,
item VARCHAR(255) NOT NULL
)
""".format(tablename)
cur.execute(command)
推荐答案
导致此错误的问题是因为您忘记在 {1!s} 和
{2!s}
,并且您也没有转义字符串'someentry'
,因此postgres
The problems that cause this error are because you forgot to add a comma between
{1!s}
and {2!s}
, and you also didn't escape the string 'someentry'
so postgres thought it was a column name identifier.
解决方案是修复语法错误和转义值。这是执行此操作的正确方法:
The solution is to fix the syntax error and escape values. Here's the correct way to do that:
cur.execute(
'INSERT INTO mytable (ip_id, item) VALUES (%s, %s)',
(1, 'someentry')
)
如果表名也是变量,则由于表名是标识符,您需要:
If the table name is also a variable, since the table name is an identifier you need to use extension
AsIs
:
from psycopg2.extensions import AsIs
cur.execute(
'INSERT INTO %s (ip_id, item) VALUES (%s, %s)',
(AsIs('mytable'), 1, 'someentry')
)
这篇关于psycopg错误,列不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!