Postgresql database error: column does not exist

Postgresql database error: column does not exist

我通过 python 使用 Postrgesql,但我在以下插入时遇到错误,声称 'column "\ufeff61356169" does not exist'.

c_id = "\ufeff61356169"
chunk = "engine"

query = f"""
    INSERT INTO company_chunks(company_id, chunk)
    VALUES(`{c_id}`, `{chunk}`);
    """
c.execute(query)

>>>
DatabaseError: {'S': 'ERROR', 'V': 'ERROR', 'C': '42703', 'M': 'column "\ufeff61356169" does not exist', 'P': '88', 'F': 'parse_relation.c', 'L': '3514', 'R': 'errorMissingColumn'}

重点说明:“\ufeff61356169”是要插入到列中的值。所以这个错误让我很困惑。它混淆了应该接收插入的列的插入值。有什么想法吗?

为了验证其他一切是否正常工作,我确保检查我的 table 是否已成功创建。

query = """
SELECT column_name 
FROM information_schema.columns 
WHERE table_name = 'company_chunks';
"""
c.execute(query)
c.fetchall()

>>>
(['company_id'], ['chunk'])

所以 table 确实存在并且它有我正在尝试插入的列。我哪里错了?

顺便说一句,我正在通过云 SQL Python 连接器连接到这个存储在 GCP 中的数据库。但是,此连接器能够创建 table,因此我认为问题是特定于 python 语法 and/or Postgres。

编辑:为了理解这个 table 的样子,这里是创建查询。

query= """
CREATE TABLE company_chunks 
    (
    company_id    VARCHAR(25)    NOT NULL,
    chunk    VARCHAR(100)    NOT NULL
    );
"""
c.execute(query)
conn.commit()

最好使用 %s 占位符:

sql = "INSERT INTO company_chunks(company_id, chunk) VALUES (%s, %s)"
var = (c_id,chunk)
mycursor.execute(sql,var)