Chinaunix首页 | 论坛 | 博客
  • 博客访问: 884004
  • 博文数量: 286
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1841
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-09 16:26
文章分类

全部博文(286)

文章存档

2016年(38)

2015年(248)

我的朋友

分类: SQLite/嵌入式数据库

2015-06-29 00:17:49


点击(此处)折叠或打开

  1. local sqlite3 = require("lsqlite3")

  2. local width = 78
  3. local function line(pref, suff)
  4.     pref = pref or ''
  5.     suff = suff or ''
  6.     local len = width - 2 - string.len(pref) - string.len(suff)
  7.     print(pref .. string.rep('-', len) .. suff)
  8. end

  9. local db, vm
  10. local assert_, assert = assert, function (test)
  11.     if (not test) then
  12.         error(db:errmsg(), 2)
  13.     end
  14. end

  15. line(sqlite3.version()) --实现在版本后加------如:3.8.7.4---------------------------------------------------------------------

  16. os.remove('test.db')
  17. db = sqlite3.open('test.db') --打开或建立test.db

  18. line(nil, 'db:exec')
  19. db:exec('CREATE TABLE t(a, b)')

  20. line(nil, 'prepare')
  21. vm = db:prepare('insert into t values(?, :bork)')
  22. assert(vm, db:errmsg())
  23. assert(vm:bind_parameter_count() == 2)
  24. assert(vm:bind_values(2, 4) == sqlite3.OK)
  25. assert(vm:step() == sqlite3.DONE)
  26. assert(vm:reset() == sqlite3.OK)
  27. assert(vm:bind_names{ 'pork', bork = 'nono' } == sqlite3.OK)
  28. assert(vm:step() == sqlite3.DONE)
  29. assert(vm:reset() == sqlite3.OK)
  30. assert(vm:bind_names{ bork = 'sisi' } == sqlite3.OK)
  31. assert(vm:step() == sqlite3.DONE)
  32. assert(vm:reset() == sqlite3.OK)
  33. assert(vm:bind_names{ 1 } == sqlite3.OK)
  34. assert(vm:step() == sqlite3.DONE)
  35. assert(vm:finalize() == sqlite3.OK)

  36. line("select * from t", 'db:exec')

  37. assert(db:exec('select * from t', function (ud, ncols, values, names)
  38.     --table.setn(values, 2)
  39.     print(unpack(values))
  40.     return sqlite3.OK
  41. end) == sqlite3.OK)

  42. line("select * from t", 'db:prepare')

  43. vm = db:prepare('select * from t')
  44. assert(vm, db:errmsg())
  45. print(vm:get_unames())
  46. while (vm:step() == sqlite3.ROW) do
  47.     print(vm:get_uvalues())
  48. end
  49. assert(vm:finalize() == sqlite3.OK)



  50. line('udf', 'scalar')

  51. local function do_query(sql)
  52.     local r
  53.     local vm = db:prepare(sql)
  54.     assert(vm, db:errmsg())
  55.     print('====================================')
  56.     print(vm:get_unames())
  57.     print('------------------------------------')
  58.     r = vm:step()
  59.     while (r == sqlite3.ROW) do
  60.         print(vm:get_uvalues())
  61.         r = vm:step()
  62.     end
  63.     assert(r == sqlite3.DONE)
  64.     assert(vm:finalize() == sqlite3.OK)
  65.     print('====================================')
  66. end

  67. local function udf1_scalar(ctx, v)
  68.     local ud = ctx:user_data()
  69.     ud.r = (ud.r or '') .. tostring(v)
  70.     ctx:result_text(ud.r)
  71. end

  72. db:create_function('udf1', 1, udf1_scalar, { })
  73. do_query('select udf1(a) from t')


  74. line('udf', 'aggregate')

  75. local function udf2_aggregate(ctx, ...)
  76.     local ud = ctx:get_aggregate_data()
  77.     if (not ud) then
  78.         ud = {}
  79.         ctx:set_aggregate_data(ud)
  80.     end
  81.     ud.r = (ud.r or 0) + 2
  82. end

  83. local function udf2_aggregate_finalize(ctx, v)
  84.     local ud = ctx:get_aggregate_data()
  85.     ctx:result_number(ud and ud.r or 0)
  86. end

  87. db:create_aggregate('udf2', 1, udf2_aggregate, udf2_aggregate_finalize, { })
  88. do_query('select udf2(a) from t')

  89. if (true) then
  90.     line(nil, '100 insert exec')
  91.     db:exec('delete from t')
  92.     local t = os.time()
  93.     for i = 1, 100 do
  94.         db:exec('insert into t values('..i..', '..(i * 2 * -1^i)..')')
  95.     end
  96.     print('elapsed: '..(os.time() - t))
  97.     do_query('select count(*) from t')

  98.     line(nil, '100000 insert exec T')
  99.     db:exec('delete from t')
  100.     local t = os.time()
  101.     db:exec('begin')
  102.     for i = 1, 100000 do
  103.         db:exec('insert into t values('..i..', '..(i * 2 * -1^i)..')')
  104.     end
  105.     db:exec('commit')
  106.     print('elapsed: '..(os.time() - t))
  107.     do_query('select count(*) from t')

  108.     line(nil, '100000 insert prepare/bind T')
  109.     db:exec('delete from t')
  110.     local t = os.time()
  111.     local vm = db:prepare('insert into t values(?, ?)')
  112.     db:exec('begin')
  113.     for i = 1, 100000 do
  114.         vm:bind_values(i, i * 2 * -1^i)
  115.         vm:step()
  116.         vm:reset()
  117.     end
  118.     vm:finalize()
  119.     db:exec('commit')
  120.     print('elapsed: '..(os.time() - t))
  121.     do_query('select count(*) from t')

  122. end

  123. line(nil, "db:close")

  124. assert(db:close() == sqlite3.OK)

  125. line(sqlite3.version())

阅读(1847) | 评论(0) | 转发(0) |
0

上一篇:LuaSQLite3 入门

下一篇:lsqlite 测试结果

给主人留下些什么吧!~~