Per this page, printf belongs to the core functions of sqlite, so, with the sqlite3 shell, I can execute this statement successfully:
sqlite> create table foo as select printf("%5.2f", 42.424242) bar;
Yet, If I try to do what I believe is the same thing with a Python script, it errors with sqlite3.OperationalError: no such function: printf. Here's the script that 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 someone know why that is?
Edit: As per g4ur4v's suggestion, I changed cur.execute('create... in the script to cur.execute(r'create.... The error did not change.
printf was added as a core function in version 3.8.3 of sqlite3. Likely the Python you are using uses an older version. This information can be found in these release notes:
2014-02-03 (3.8.3)
Added support for common table expressions and the WITH clause.
Added the printf() SQL function.
One way to check what version of the Sqlite3 library your Python is using is to issue this command:
print(sqlite3.sqlite_version)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With