colonelthirtytwo / lsqlite3-ffi Goto Github PK
View Code? Open in Web Editor NEWLua SQLite using LuaJIT's FFI library.
License: The Unlicense
Lua SQLite using LuaJIT's FFI library.
License: The Unlicense
Hi,
In case of the BLOB type get_value() returns two values
return sqlite3.sqlite3_column_blob(stmt,n), sqlite3.sqlite3_column_bytes(stmt,n)
Which is later in get_values() function lost, so only pointer to blob is kept, the size is discarded by assignment.
function sqlite_stmt:get_values()
local tbl = {}
for i=0,sqlite3.sqlite3_column_count(self.stmt)-1 do
tbl[i+1] = self:get_value(i)
-- ^^^^ here is the problem
end
return tbl
end
Test case:
Database:
CREATE TABLE test(v1 VARCHAR(20), v2 BLOB);
INSERT INTO test VALUES('abcd',X'ABCD');
Lua:
local sqlite = require 'lsqlite3'
local conn = sqlite.open('testdb.sqlite', "read")
local stmt = conn:prepare 'select v1,hex(v2) from test'
while stmt:step() do
print(stmt:get_value(0), stmt:get_value(1))
end
stmt:finalize()
conn:close()
Actual output:
abc ABC
Expected output:
abcd ABCD
Fix:
--- lsqlite3.lua.bad 2017-03-23 20:34:53.000000000 +0100
+++ lsqlite3.lua.good 2017-03-23 20:45:58.000000000 +0100
@@ -995,7 +995,7 @@
local value_handlers = {
[sqlite3.SQLITE_INTEGER] = function(stmt, n) return sqlite3.sqlite3_column_int(stmt, n) end,
[sqlite3.SQLITE_FLOAT] = function(stmt, n) return sqlite3.sqlite3_column_double(stmt, n) end,
- [sqlite3.SQLITE_TEXT] = function(stmt, n) return ffi.string(sqlite3.sqlite3_column_text(stmt,n), sqlite3.sqlite3_column_bytes(stmt,n)-1) end,
+ [sqlite3.SQLITE_TEXT] = function(stmt, n) return ffi.string(sqlite3.sqlite3_column_text(stmt,n), sqlite3.sqlite3_column_bytes(stmt,n)) end,
[sqlite3.SQLITE_BLOB] = function(stmt, n) return sqlite3.sqlite3_column_blob(stmt,n), sqlite3.sqlite3_column_bytes(stmt,n) end,
[sqlite3.SQLITE_NULL] = function() return nil end
}
I would like to make use of the sqlite3 ffi bindings in an APGLv3 project. Since no license is defined this is impossible.
Des this code helps ?
function sqlite_db:prepare(sql)
local stmtptr = new_stmt_ptr()
self:check(sqlite3.sqlite3_prepare_v2(self.db, sql, #sql+1, stmtptr, nil))
-- Following line is appropriate cleanup ?
ffi.gc(stmtptr, sqlite3.sqlite3_finalize)
local stmt = setmetatable(
{
stmt=stmtptr[0],
db=self,
trace=lsqlite3.DEBUG and debug.traceback() or nil
},sqlite_stmt)
self.stmts[stmt] = stmt
return stmt
end
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.