简体   繁体   中英

Postgresql database error: column does not exist

I'm using Postrgesql via python, but I'm getting an error on the following insertion, alleging that 'column "\61356169" 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'}

One key note : "\61356169" is the value which is to be inserted into the column. So the error confuses me. It's confusing the insertion value for the column, which should receive the insertion. Any thoughts?

Just to verify that everything else is in working order I made sure to check that my table was successfully created.

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

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

So the table does exist and it has the columns, which I'm trying to make insertions to. Where am I going wrong here?

Btw, I'm connecting to this database, which is stored in GCP, via the Cloud SQL Python Connector. However, this connector was able to create the table, so I believe the problem is specific to python syntax and/or Postgres.

Edit: For the sake of understanding what this table looks like, here's the creation query.

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

better to do it this way by using %s placeholder:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM