insert + upsert works in sqlite3 console, but on python it shows syntax error

时间:2018-12-03 13:12:47

标签: python sqlite sql-insert upsert

I'm using sqlite version 3.25.1. When I execute the following query in sqlite console, it works:

insert into cases (bi, age, shape, margin, density, severity) 
values (5, 67, 3, 5, 3, 1)
on conflict(bi, age, shape, margin, density, severity) 
DO UPDATE SET frequency=frequency+1;

But when I execute it on python, it says:

sqlite3.OperationalError: near "on": syntax error

Here is my code on python:

C.execute('insert into cases (bi, age, shape, margin, density, severity) '
          'values (5, 67, 3, 5, 3, 1)'
          'on conflict(bi, age, shape, margin, density, severity) '
          'DO UPDATE SET frequency=frequency+1', ())

I don't know how to make it work.

1 个答案:

答案 0 :(得分:2)

It's possible UPSERT is not included in the sqlite3 version that python loads. From the sqlite3 doc:

UPSERT syntax was added to SQLite with version 3.24.0 (2018-06-04).

To find the sqlite version in python:

>>> import sqlite3
>>> sqlite3.sqlite_version