GithubHelp home page GithubHelp logo

lsqlite's Introduction

lsqlite

sqlite3的lua封装库!

依赖

  • lua5.2以上
  • luakit一个luabind库
  • 项目路径如下
    |--proj
     |--lua
     |--lsqlite
     |--luakit

编译

  • msvc: 准备好lua依赖库并放到指定位置,将proj文件加到sln后编译。
  • linux: 准备好lua依赖库并放到指定位置,执行make -f lmdb.mak

注意事项

  • mimalloc: 参考quanta使用,不用则在工程文件中注释

用法

-- sqlite_test.lua
local sformat       = string.format
local log_debug     = logger.debug
local jsoncodec     = json.jsoncodec

local driver = sqlite.create()
local jcodec = jsoncodec()

driver.set_codec(jcodec)
driver.open("./sqlite/xxx.db")

local c, ce = driver.exec("CREATE TABLE PLAYER (KEY INT PRIMARY KEY NOT NULL, VALUE BLOB);")
log_debug("CREATE: {}:{}", c, ce)

local i, ie = driver.exec("INSERT INTO PLAYER (KEY, VALUE) VALUES (1, 'Paul1');")
log_debug("INSERT: {}:{}", i, ie)
i, ie = driver.exec("INSERT INTO PLAYER (KEY, VALUE) VALUES (2, 'Paul2');")
log_debug("INSERT: {}:{}", i, ie)
i, ie = driver.exec("INSERT INTO PLAYER (KEY, VALUE) VALUES (3, 'Paul3');")
log_debug("INSERT: {}:{}", i, ie)
i, ie = driver.exec("INSERT INTO PLAYER (KEY, VALUE) VALUES (3, 'Paul33');")
log_debug("INSERT: {}:{}", i, ie)

for j = 1, 6 do
    local rc, roe = driver.find(sformat("SELECT * FROM PLAYER WHERE KEY = '%s'", j))
    log_debug("SELECT1-{}: {}-{}", j, rc, roe)
end

local r, re = driver.exec("REPLACE INTO PLAYER (KEY, VALUE) VALUES (3, 'Paul33');")
log_debug("REPLACE: {}:{}", r, re)
r, re = driver.exec("REPLACE INTO PLAYER (KEY, VALUE) VALUES (4, 'Paul4');")
log_debug("REPLACE: {}:{}", r, re)
local u, ue = driver.exec("UPDATE PLAYER set VALUE ='Paul222' where KEY=2")
log_debug("UPDATE: {}:{}", u, ue)
u, ue = driver.exec("UPDATE PLAYER set VALUE ='Paul222' where KEY=5")
log_debug("UPDATE: {}:{}", u, ue)
local d, de = driver.exec("DELETE FROM PLAYER where KEY=1")
log_debug("DELETE: {}:{}", d, de)
d, de = driver.exec("DELETE FROM PLAYER where KEY=5")
log_debug("DELETE: {}:{}", d, de)

for j = 1, 6 do
    local rc, roe = driver.find(sformat("SELECT * FROM PLAYER WHERE KEY = %s", j))
    log_debug("SELECT2-{}: {}-{}", j, rc, roe)
end

local dd, dde = driver.exec("DROP TABLE PLAYER")
log_debug("DROP: {}:{}", dd, dde)

--stmt
local scc, st_create = driver.prepare("CREATE TABLE PLAYER (KEY INT PRIMARY KEY NOT NULL, VALUE BLOB);")
log_debug("prepare CREATE: {}:{}", scc, st_create)
local sc, sce = st_create.exec()
log_debug("SCREATE: {}:{}", sc, sce)

local sic, st_insert = driver.prepare("INSERT INTO PLAYER (KEY, VALUE) VALUES (?, ?);")
log_debug("prepare INSERT: {}:{}", sic, st_insert)
local src, st_replace = driver.prepare("REPLACE INTO PLAYER (KEY, VALUE) VALUES (?, ?);")
log_debug("prepare REPLACE: {}:{}", src, st_replace)
local suc, st_update = driver.prepare("UPDATE PLAYER set VALUE = ? where KEY=?")
log_debug("prepare UPDATE: {}:{}", suc, st_update)
local ssc, st_select = driver.prepare("SELECT * FROM PLAYER WHERE KEY = ?")
log_debug("prepare SELECT: {}:{}", ssc, st_select)
local sdc, st_delete = driver.prepare("DELETE FROM PLAYER where KEY=?")
log_debug("prepare DELETE: {}:{}", sdc, st_delete)

for j = 1, 3 do
    st_insert.bind(1, j)
    st_insert.bind(2, "aaa" .. j)
    local cc, cce = st_insert.exec()
    log_debug("st_insert: {} => {}:{}", j, cc, cce)
end
st_insert.bind(1, 3)
st_insert.bind(2, "aaa4")
local icc, icce = st_insert.exec()
log_debug("st_insert: 3 => {}:{}", icc, icce)

for j = 4, 5 do
    local cc, cce = st_insert.run(j, {value = j})
    log_debug("st_insert: {} => {}:{}", j, cc, cce)
end

st_select.bind(1, 4)
local secc, item = st_select.exec()
log_debug("st_select: => {}:{}", secc, item)
local secc2, item2 = st_select.run(5)
log_debug("st_select: => {}:{}", secc2, item2)

lsqlite's People

Contributors

xiyoo0812 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.