如何使用sqlite3作为回调函数所需的python函数的ctypes进行编码和换行?
我正在尝试从python执行 sqlite3_exec
以逐行提取数据库的内容。根据 C API ,我需要一个回调函数来执行迭代。我在互联网上提供了许多帮助,对以下代码进行了编码:
I'm trying to perform sqlite3_exec
from python to extract line by line the contents of a database. According to the C API, I need a Callback function which will perform the iteration. I have coded the following with a lot of help from internet:
已使用@eryksun建议进行了更新
import ctypes
def extractor(unused, num_columns, pcolumn, pcolumn_name):
for column in range(0,num_columns):
if pcolumn[i] != None:
print pcolumn[i]
sqlite3DLL = ctypes.CDLL("C:\\Python\\PYTHON\\DLLs\\sqlite3.dll")
SQLITE_OPEN_READONLY = 1
null_ptr = ctypes.c_void_p(None)
p_src_db = ctypes.c_void_p(None)
ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_char_p))
callback_func = callback_type(extractor)
connect = sqlite3DLL.sqlite3_open_v2(DatabasePath, ctypes.byref(p_src_db), SQLITE_OPEN_READONLY, null_ptr)
connect = sqlite3DLL.sqlite3_exec(DatabasePath, "SELECT * FROM *", callback_func, None, null_ptr)
sqlite3DLL.sqlite3_close(DatabasePath)
在继续使用python回调函数之前,我有一些疑问:
Before moving on to the python callback function I have some doubts:
-
SELECT * FROM *
是否可能是 SQL语句,以避免提供表名(因为我不知道它的名字) - 两个函数的第一个参数
sqlite3_open_v2
和sqlite3_exec
是数据库的路径?
- Is
"SELECT * FROM *"
a possible SQL statement to avoid providing the name of a table (because I do not know it)? - Is the first argument of both functions
sqlite3_open_v2
andsqlite3_exec
the path to the database?
如果一切正常,我们可以继续使用回调函数。根据我在网上发现的内容,C回调函数应与以下类似:
If all that is ok we can move on to the callback function. According to what I found on the net, the C callback function should be somewhat similar to:
callback(void *NotUsed, int argc, char **argv, char **azColName)
for (int i = 0; i < argc; i++) {printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL")}
这就是为什么我将 CFUNCTYPE 。我该如何编码与可能用数据库内容填充列表的回调函数相匹配的python函数?
That is why I coded the CFUNCTYPE
you see on my code. How can I code a python function which matches the callback needed to maybe fill a list with the database contents?
我已在代码中添加了建议的更改,即回调函数只是打印该值以验证输出。但这无法正常工作,但我得到一个错误:
I have added the proposed changes on the code, the callback function just prints the value to verify the output. But It wont work I get an error:
con = sqlite3DLL.sqlite3_exec(FastenerLibraryPath, "SELECT * FROM *", callback_func, None, null_ptr)
WindowsError: exception: access violation writing 0x0000000000000009
非常感谢!
最终版本(@eryksun注释和@MarkTolonen解决方案)
import ctypes
def extractor(unused, num_columns, pcolumn, pcolumn_name):
print ','.join(["''" if x is None else "'"+x+"'" for x in pcolumn[:num_columns]])
return 0
sqlite3DLL = ctypes.CDLL("C:\\Python\\PYTHON\\DLLs\\sqlite3.dll")
SQLITE_OPEN_READONLY = 1
null_ptr = ctypes.c_void_p(None)
p_src_db = ctypes.c_void_p(None)
ctypes.CFUNCTYPE(ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_char_p))
callback_func = callback_type(extractor)
connect = sqlite3DLL.sqlite3_open_v2(DatabasePath, ctypes.byref(p_src_db), SQLITE_OPEN_READONLY, None)
connect = sqlite3DLL.sqlite3_exec(p_src_db, b"SELECT * FROM Pin", callback_func, None, None)
sqlite3DLL.sqlite3_close(p_src_db)
这是可行的,我将研究 argtypes
和不透明类型。
This is working, however, I will look into argtypes
and the opaque type.
谢谢大家!
使用以下数据库在Python 2.7和Python 3.6(如果更改DLL路径)中进行了测试:
Tested in Python 2.7 and Python 3.6 (if you change the DLL path) using the following database:
create table tbl1(one varchar(10), two smallint);
insert into tbl1 values('hello',10);
insert into tbl1 values('goodbye',20);
代码:
# I know, bad form, but it makes the code easier to read for an example
from ctypes import *
# This was missing the 2nd c_int parameter.
CALLBACK = CFUNCTYPE(c_int, c_void_p, c_int, POINTER(c_char_p), POINTER(c_char_p))
@CALLBACK
def extractor(unused, num_columns, pcolumn, pcolumn_name):
print(pcolumn[:num_columns])
return 0 # needs to return 0 from callback or will abort.
sqlite3DLL = CDLL(r"C:\Python27\DLLs\sqlite3.dll")
SQLITE_OPEN_READONLY = 1
p_src_db = c_void_p()
sqlite3DLL.sqlite3_open_v2(b'test.db', byref(p_src_db), SQLITE_OPEN_READONLY, None)
# pass the handle returned by above as first parameter below
sqlite3DLL.sqlite3_exec(p_src_db, b'SELECT * FROM tbl1', extractor, None, None)
sqlite3DLL.sqlite3_close(p_src_db)
输出:
['hello', '10']
['goodbye', '20']
我还建议设置 argtypes
,因为它有助于捕获
I also recommend setting argtypes
because it helps catch type errors and for some parameter types (like c_double) it is required.
sqlite3DLL.sqlite3_open_v2.argtypes = c_char_p, POINTER(c_void_p), c_int,c_char_p
sqlite3DLL.sqlite3_open_v2.restype = c_int
sqlite3DLL.sqlite3_exec.argtypes = c_void_p,c_char_p,CALLBACK,c_void_p,POINTER(c_char_p)
sqlite3DLL.sqlite3_open_v2.restype = c_int