Why can't I use printf with sqlite when executed in Python
Per this page , printf
belongs to the core functionality of sqlite, so with the shell sqlite3
I can execute this statement successfully:
sqlite> create table foo as select printf("%5.2f", 42.424242) bar;
However, if I try to do what I believe is the same with a Python script, these are errors with sqlite3.OperationalError: no such function: printf
. Here's the script I used:
# -*- coding: utf-8 -*-
import os
import sys
import sqlite3
db_filename = 'printf.db'
if os.path.isfile(db_filename):
os.remove(db_filename)
db = sqlite3.connect(db_filename)
db.text_factory = str
cur = db.cursor()
cur.execute(r'create table foo as select printf("%5.2f", 42.424242) bar')
Does anyone know why this is?
Edit . As suggested by g4ur4v, I changed cur.execute('create...
the script to cur.execute(r'create...
. The error has not changed.
source to share
printf
was added as a main feature in version 3.8.3 of sqlite3
. The Python you are using is probably using an older version. This information can be found in these issues :
2014-02-03 (3.8.3)
Added support for common table expressions and WITH clause.
Added SQL printf () function .
One way to check which version of the Sqlite3 library your Python is using is to run the following command:
print(sqlite3.sqlite_version)
source to share