Search code examples
pythonsqlsqlitepeewee

peewee raw query does not work with WITH statement


I have query which works in SQLite Spy (a tool for viewing SQLite Databases) but I can't get the query to work in python with peewee as a raw query.

The (simplified) query is

WITH tags(name) AS ( VALUES('foo'),('bar') ) SELECT * FROM tags

The (simplified) python code is:

from peewee import *

db = SqliteDatabase(":memory:")
db.execute_sql("WITH tags(name) AS ( VALUES('foo'),('bar') ) SELECT * FROM tags")

I get an error

peewee.OperationalError: near "WITH": syntax error

I also tried the RawQuery function or using SqliteExtDatabase from the PlayHouse extension.

Is this error caused by me or is it a problem of peewee?


Solution

  • CTE was introduced in SQLite as of version 3.8.3

    http://www.sqlite.org/releaselog/3_8_3.html

    it is very likely that your Python is using a previous version that does not support WITH Check sqlite3.sqlite_version_info

    https://docs.python.org/2/library/sqlite3.html