I keep getting this
error: psycopg2.ProgrammingError: column "someentry" does not exist.
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)
You have to use single quotes within the query.
I received the same type of error from this
it produced
Obviously it is data, not a column name, like you said.
When I changed it to
it worked with no errors.
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:
If the table name is also a variable, since the table name is an identifier you need to use extension
AsIs
: