1

I'm curious, how to debug SQLite3 syntax. I work with this SQL on daily bases and I have sometimes syntax error which I can't repair.

For example this query:

    self.cur.execute("""INSERT INTO table(?,?,?,?,?,?,?,?,?,?,?,?,?) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)""", ('category', 'product_number', 'product', 'availability', 'manufacturer', 'weight', 'inner', 'outer', 'width', 'list_price', 'discount', 'gross_price', 'net_price', u'Wellenscheiben BKAGI..', 'BKAGI15', '-', 'This', 'PePS', '138,00', '50,00', '78,00', '6,50', '36,93', '55,00', '19,78', '16,62'))

sqlite3.OperationalError: near "?": syntax error

Do you know how to find out where the error is?

1
  • You can't (and shouldn't) use query parameters for identifiers (table and column names). Commented Nov 30, 2015 at 13:58

3 Answers 3

2

Have you tried setting this flag:

sqlite3.enable_callback_tracebacks(True)

Here are the docs: https://docs.python.org/2/library/sqlite3.html#sqlite3.enable_callback_tracebacks

EDIT: tried myself, doesn't help much in that case. You could also try to log the executed sql, like it is explained here: https://stackoverflow.com/a/13647368/1338845 and then see if that sql is what it should be.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm unclear on whether you just want general debugging advice or help specifically for that issue. If the latter:

It appears that you can only use the ? placeholders for values, and not for column names. Either include the columns directly in the string, or if you're trying to cut down on the length of the statement for readability, something like:

cols = ','.join('category', 'product_number', 'product', 'availability', 'manufacturer', 'weight', 'inner', 'outer', 'width', 'list_price', 'discount', 'gross_price', 'net_price')
self.cur.execute('INSERT INTO table({}) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'.format(cols), (u'Wellenscheiben BKAGI..', 'BKAGI15', '-', 'This', 'PePS', '138,00', '50,00', '78,00', '6,50', '36,93', '55,00', '19,78', '16,62'))

Comments

0

The rule is generally to open a terminal (console under Windows) and type the command in sqlite3 (or sqlite3.exe).

Or course it is little use when the problem is in variable replacement like here. But SQL only allow such a variable replacement for values, not for table or column names.

So for your example, the correct syntax is:

self.cur.execute("""INSERT INTO table(category, product_number, product,
        availability, manufacturer, weight, inner, outer, width, list_price,
        discount, gross_price, net_price) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)""",
    u"Wellenscheiben BKAGI..", "BKAGI15', '-', 'This', 'PePS', '138,00', '50,00', 
    '78,00', '6,50', '36,93', '55,00', '19,78', '16,62'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.