Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable substitution in SQLite Python

Tags:

python

sqlite

I have the following Python code and it gives me errors when I execute it, the error says

sqlite3.OperationalError: near "%": syntax error

statement = "INSERT INTO %s (%s) VALUES " % (table,columns) + "(%s,%s,%s);"
cur.execute(statement, (index,fullpath,filename))
like image 522
kartoza-geek Avatar asked Nov 20 '25 23:11

kartoza-geek


1 Answers

SQL parameters are handled by the database, not by Python, so the syntax is not necessarily the same as Python's.

In SQLite (and most other databases), parameters are marked with a ?:

statement = "INSERT INTO %s (%s) VALUES (?,?,?);" % (table,columns)
cur.execute(statement, (index,fullpath,filename))
like image 133
CL. Avatar answered Nov 22 '25 13:11

CL.