psycopg 错误,列不存在
psycopg error, column does not exist
我一直收到这个
error: psycopg2.ProgrammingError: column "someentry" does not exist.
错误表明当 someentry
不是列时,someentry
列不存在,它只是输入到数据库中的一个值。
这是给出错误的代码:
cur.execute('INSERT INTO {0!s} (ip_id, item) VALUES ({1!s}{2!s})'.format('mytable',1,'someentry'))
以下是我创建 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认为是一个列名标识符。
解决方法是修复语法错误和转义值。这是正确的方法:
cur.execute(
'INSERT INTO mytable (ip_id, item) VALUES (%s, %s)',
(1, 'someentry')
)
如果 table 名称也是一个变量,因为 table 名称是一个标识符,您需要 use extension AsIs
:
from psycopg2.extensions import AsIs
cur.execute(
'INSERT INTO %s (ip_id, item) VALUES (%s, %s)',
(AsIs('mytable'), 1, 'someentry')
)
您必须在查询中使用单引号。
我收到了同样类型的错误
cur.execute('insert into my_table(id, name, horse_type, horse_code, horse_name) values(default, %s, 3, %s, "Mary Wonder")', [e[0], e[1]])
产生了
Traceback (most recent call last):
File "process_horse.py", line 11, in <module>
[e[0], e[1]])
psycopg2.ProgrammingError: column "Mary Wonder" does not exist
LINE 2: ', "Mary Wonder")
^
很明显是数据,不是你说的列名
当我把它改成
cur.execute("insert into my_table(id, name, horse_type, horse_code, horse_name) values(default, %s, 3, %s, 'Mary Wonder')",[e[0], e[1]])
它没有错误。
我一直收到这个
error: psycopg2.ProgrammingError: column "someentry" does not exist.
错误表明当 someentry
不是列时,someentry
列不存在,它只是输入到数据库中的一个值。
这是给出错误的代码:
cur.execute('INSERT INTO {0!s} (ip_id, item) VALUES ({1!s}{2!s})'.format('mytable',1,'someentry'))
以下是我创建 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认为是一个列名标识符。
解决方法是修复语法错误和转义值。这是正确的方法:
cur.execute(
'INSERT INTO mytable (ip_id, item) VALUES (%s, %s)',
(1, 'someentry')
)
如果 table 名称也是一个变量,因为 table 名称是一个标识符,您需要 use extension AsIs
:
from psycopg2.extensions import AsIs
cur.execute(
'INSERT INTO %s (ip_id, item) VALUES (%s, %s)',
(AsIs('mytable'), 1, 'someentry')
)
您必须在查询中使用单引号。
我收到了同样类型的错误
cur.execute('insert into my_table(id, name, horse_type, horse_code, horse_name) values(default, %s, 3, %s, "Mary Wonder")', [e[0], e[1]])
产生了
Traceback (most recent call last):
File "process_horse.py", line 11, in <module>
[e[0], e[1]])
psycopg2.ProgrammingError: column "Mary Wonder" does not exist
LINE 2: ', "Mary Wonder")
^
很明显是数据,不是你说的列名
当我把它改成
cur.execute("insert into my_table(id, name, horse_type, horse_code, horse_name) values(default, %s, 3, %s, 'Mary Wonder')",[e[0], e[1]])
它没有错误。