2017-08-28 21:31:44 +00:00
|
|
|
import sys
|
2015-06-05 15:39:09 +00:00
|
|
|
import ffilib
|
2014-04-17 00:35:29 +00:00
|
|
|
|
|
|
|
|
2015-06-05 15:39:09 +00:00
|
|
|
sq3 = ffilib.open("libsqlite3")
|
2014-04-17 00:35:29 +00:00
|
|
|
|
|
|
|
sqlite3_open = sq3.func("i", "sqlite3_open", "sp")
|
2021-05-27 05:50:04 +00:00
|
|
|
# int sqlite3_close(sqlite3*);
|
2014-06-08 21:53:55 +00:00
|
|
|
sqlite3_close = sq3.func("i", "sqlite3_close", "p")
|
2021-05-27 05:50:04 +00:00
|
|
|
# int sqlite3_prepare(
|
2014-04-17 00:35:29 +00:00
|
|
|
# sqlite3 *db, /* Database handle */
|
|
|
|
# const char *zSql, /* SQL statement, UTF-8 encoded */
|
|
|
|
# int nByte, /* Maximum length of zSql in bytes. */
|
|
|
|
# sqlite3_stmt **ppStmt, /* OUT: Statement handle */
|
|
|
|
# const char **pzTail /* OUT: Pointer to unused portion of zSql */
|
2021-05-27 05:50:04 +00:00
|
|
|
# );
|
2014-04-17 00:35:29 +00:00
|
|
|
sqlite3_prepare = sq3.func("i", "sqlite3_prepare", "psipp")
|
2021-05-27 05:50:04 +00:00
|
|
|
# int sqlite3_finalize(sqlite3_stmt *pStmt);
|
2014-06-08 21:53:55 +00:00
|
|
|
sqlite3_finalize = sq3.func("i", "sqlite3_finalize", "p")
|
2021-05-27 05:50:04 +00:00
|
|
|
# int sqlite3_step(sqlite3_stmt*);
|
2014-04-17 00:35:29 +00:00
|
|
|
sqlite3_step = sq3.func("i", "sqlite3_step", "p")
|
2021-05-27 05:50:04 +00:00
|
|
|
# int sqlite3_column_count(sqlite3_stmt *pStmt);
|
2014-04-17 00:35:29 +00:00
|
|
|
sqlite3_column_count = sq3.func("i", "sqlite3_column_count", "p")
|
2021-05-27 05:50:04 +00:00
|
|
|
# int sqlite3_column_type(sqlite3_stmt*, int iCol);
|
2014-04-17 00:35:29 +00:00
|
|
|
sqlite3_column_type = sq3.func("i", "sqlite3_column_type", "pi")
|
|
|
|
sqlite3_column_int = sq3.func("i", "sqlite3_column_int", "pi")
|
|
|
|
# using "d" return type gives wrong results
|
2014-09-10 20:06:54 +00:00
|
|
|
sqlite3_column_double = sq3.func("d", "sqlite3_column_double", "pi")
|
2014-04-17 00:35:29 +00:00
|
|
|
sqlite3_column_text = sq3.func("s", "sqlite3_column_text", "pi")
|
2021-05-27 05:50:04 +00:00
|
|
|
# sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
|
2014-06-16 23:22:16 +00:00
|
|
|
# TODO: should return long int
|
|
|
|
sqlite3_last_insert_rowid = sq3.func("i", "sqlite3_last_insert_rowid", "p")
|
2021-05-27 05:50:04 +00:00
|
|
|
# const char *sqlite3_errmsg(sqlite3*);
|
2014-06-08 21:53:55 +00:00
|
|
|
sqlite3_errmsg = sq3.func("s", "sqlite3_errmsg", "p")
|
2014-04-17 00:35:29 +00:00
|
|
|
|
2014-06-08 21:53:55 +00:00
|
|
|
# Too recent
|
|
|
|
##const char *sqlite3_errstr(int);
|
2021-05-27 05:50:04 +00:00
|
|
|
# sqlite3_errstr = sq3.func("s", "sqlite3_errstr", "i")
|
2014-04-17 00:35:29 +00:00
|
|
|
|
2014-06-08 21:53:55 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
SQLITE_OK = 0
|
|
|
|
SQLITE_ERROR = 1
|
|
|
|
SQLITE_BUSY = 5
|
|
|
|
SQLITE_MISUSE = 21
|
|
|
|
SQLITE_ROW = 100
|
|
|
|
SQLITE_DONE = 101
|
2014-04-17 00:35:29 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
SQLITE_INTEGER = 1
|
|
|
|
SQLITE_FLOAT = 2
|
|
|
|
SQLITE_TEXT = 3
|
|
|
|
SQLITE_BLOB = 4
|
|
|
|
SQLITE_NULL = 5
|
2014-04-17 00:35:29 +00:00
|
|
|
|
|
|
|
|
2014-06-08 21:53:55 +00:00
|
|
|
class Error(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def check_error(db, s):
|
|
|
|
if s != SQLITE_OK:
|
|
|
|
raise Error(s, sqlite3_errmsg(db))
|
|
|
|
|
|
|
|
|
2014-04-17 00:35:29 +00:00
|
|
|
class Connections:
|
|
|
|
def __init__(self, h):
|
|
|
|
self.h = h
|
|
|
|
|
|
|
|
def cursor(self):
|
|
|
|
return Cursor(self.h)
|
|
|
|
|
2014-06-08 21:53:55 +00:00
|
|
|
def close(self):
|
|
|
|
s = sqlite3_close(self.h)
|
|
|
|
check_error(self.h, s)
|
|
|
|
|
2014-04-17 00:35:29 +00:00
|
|
|
|
|
|
|
class Cursor:
|
|
|
|
def __init__(self, h):
|
|
|
|
self.h = h
|
2014-06-08 21:53:55 +00:00
|
|
|
self.stmnt = None
|
2014-04-17 00:35:29 +00:00
|
|
|
|
2014-06-08 22:53:21 +00:00
|
|
|
def execute(self, sql, params=None):
|
|
|
|
if params:
|
|
|
|
params = [quote(v) for v in params]
|
|
|
|
sql = sql % tuple(params)
|
|
|
|
print(sql)
|
2014-04-17 00:35:29 +00:00
|
|
|
b = bytearray(4)
|
2014-06-08 21:53:55 +00:00
|
|
|
s = sqlite3_prepare(self.h, sql, -1, b, None)
|
|
|
|
check_error(self.h, s)
|
2017-08-28 21:31:44 +00:00
|
|
|
self.stmnt = int.from_bytes(b, sys.byteorder)
|
2021-05-27 05:50:04 +00:00
|
|
|
# print("stmnt", self.stmnt)
|
2014-06-08 21:53:55 +00:00
|
|
|
self.num_cols = sqlite3_column_count(self.stmnt)
|
2021-05-27 05:50:04 +00:00
|
|
|
# print("num_cols", self.num_cols)
|
2014-06-08 22:59:02 +00:00
|
|
|
# If it's not select, actually execute it here
|
2014-06-16 21:50:08 +00:00
|
|
|
# num_cols == 0 for statements which don't return data (=> modify it)
|
|
|
|
if not self.num_cols:
|
|
|
|
v = self.fetchone()
|
|
|
|
assert v is None
|
2014-06-16 23:22:16 +00:00
|
|
|
self.lastrowid = sqlite3_last_insert_rowid(self.h)
|
2014-06-08 21:53:55 +00:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
s = sqlite3_finalize(self.stmnt)
|
|
|
|
check_error(self.h, s)
|
2014-04-17 00:35:29 +00:00
|
|
|
|
|
|
|
def make_row(self):
|
|
|
|
res = []
|
|
|
|
for i in range(self.num_cols):
|
2014-06-08 21:53:55 +00:00
|
|
|
t = sqlite3_column_type(self.stmnt, i)
|
2021-05-27 05:50:04 +00:00
|
|
|
# print("type", t)
|
2014-04-17 00:35:29 +00:00
|
|
|
if t == SQLITE_INTEGER:
|
2014-06-08 21:53:55 +00:00
|
|
|
res.append(sqlite3_column_int(self.stmnt, i))
|
2014-04-17 00:35:29 +00:00
|
|
|
elif t == SQLITE_FLOAT:
|
2014-06-08 21:53:55 +00:00
|
|
|
res.append(sqlite3_column_double(self.stmnt, i))
|
2014-04-17 00:35:29 +00:00
|
|
|
elif t == SQLITE_TEXT:
|
2014-06-08 21:53:55 +00:00
|
|
|
res.append(sqlite3_column_text(self.stmnt, i))
|
2014-04-17 00:35:29 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
return tuple(res)
|
|
|
|
|
|
|
|
def fetchone(self):
|
2014-06-08 21:53:55 +00:00
|
|
|
res = sqlite3_step(self.stmnt)
|
2021-05-27 05:50:04 +00:00
|
|
|
# print("step:", res)
|
2014-04-17 00:35:29 +00:00
|
|
|
if res == SQLITE_DONE:
|
|
|
|
return None
|
|
|
|
if res == SQLITE_ROW:
|
|
|
|
return self.make_row()
|
2014-06-08 21:53:55 +00:00
|
|
|
check_error(self.h, res)
|
2014-04-17 00:35:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def connect(fname):
|
|
|
|
b = bytearray(4)
|
|
|
|
sqlite3_open(fname, b)
|
2017-08-28 21:31:44 +00:00
|
|
|
h = int.from_bytes(b, sys.byteorder)
|
2014-04-17 00:35:29 +00:00
|
|
|
return Connections(h)
|
2014-06-08 22:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def quote(val):
|
|
|
|
if isinstance(val, str):
|
|
|
|
return "'%s'" % val
|
|
|
|
return str(val)
|