JSA里的FFI(3)——sqlite3

wils
wils

创作者俱乐部成员

今天继续猜谜🤣

win10以上系统自带winsqlite3.dll,所以自从JSA有了FFI,就可以直接操作sqlite3数据库了


我猜出了打开关闭的大概用法,群里石头大佬完善了插入表,在这里尽量解释一下猜出来的用法:


🔔

function tt()

{

const sqlite3 = FFI.LoadLibrary('winsqlite3')

const sqlite3_open = sqlite3.LoadFunction('', 'sqlite3_open', '', 'int32', ['string', 'pointer'])

const sqlite3_exec = sqlite3.LoadFunction('', 'sqlite3_exec', '', 'int32', ['pointer', 'string', 'pointer', 'pointer', 'pointer'])

const sqlite3_close = sqlite3.LoadFunction('', 'sqlite3_close', '', 'int32', ['pointer'])

const db_ptr = FFI.Malloc(FFI.PointerSize)

sqlite3_open.Call(`${ThisWorkbook.Path}/a.db`, db_ptr)

const db = db_ptr.Read('pointer', 0)

const null_ptr = FFI.BigIntToPointer(0)

sqlite3_exec.Call(db, `CREATE TABLE IF NOT EXISTS test (

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT

)`, null_ptr, null_ptr, null_ptr)

sqlite3_exec.Call(db, `INSERT INTO test (name) VALUES

('A'),

('B')

`, null_ptr, null_ptr, null_ptr)

sqlite3_close.Call(db)

FFI.Free(db_ptr)

}

  • FFI.LoadLibrary导入所需要的dll这里因为win10以上系统自带,且32位64位都有,所以直接写

  • LoadFunction导入dll里所需要的方法,方法的返回值类型和参数类型列表可以尝试用int8 int16 int32 long string pointer等

  • 根据sqlite3的手册,open时需要开辟一块保存指向数据库指针的指针,所以FFI.Malloc(FFI.PointerSize)

  • open之后,db_ptr.Read('pointer', 0)里的Read是取指针指向的具体内容,这里取指向数据库的指针,所以第一个参数表示内容的类型为指针

  • 需要空指针时,可以直接传递int32类型的数字0,这里完全靠名字猜FFI.BigIntToPointer(0)应该也是传递了一个空指针


至于需要读取时,我猜可能需要用ArrayBuffer读数组

我很菜,希望更多朋友来指正交流尝试


有了FFI,宏的窗体甚至都不用提供了,直接调用winapi创建窗口恐怕更灵活方便,就像ahk里的gui,只是封装成js可用的包估计要写不少代码,有没有感兴趣的朋友有需要啊🤣


官方快出文档,出了FFI文档,JSA就起飞了🎉

海南省
浏览 56
收藏
3
分享
3 +1
2
+1
全部评论 2
 
Joy
需要哦
· 山东省
回复
 
恰同学少年
· 黑龙江省
回复