sqlite.lua revision 1.3
1-- $NetBSD: sqlite.lua,v 1.3 2015/12/08 23:04:40 kamil Exp $
2
3local sqlite = require 'sqlite'
4
5print(sqlite._VERSION .. ' - ' .. sqlite._DESCRIPTION)
6print(sqlite._COPYRIGHT)
7print()
8
9print('initialize sqlite')
10sqlite.initialize()
11
12print('this is sqlite ' .. sqlite.libversion() .. ' (' ..
13    sqlite.libversion_number() .. ')')
14print('sourceid ' .. sqlite.sourceid())
15
16db, state = sqlite.open('/tmp/db.sqlite', sqlite.OPEN_CREATE)
17
18if state ~= sqlite.OK then
19	print('db open failed')
20else
21	err = db:exec('create table test (name varchar(32))')
22
23	if err ~= sqlite.OK then
24		print('table creation failed')
25		print('error code ' .. db:errcode() .. ' msg ' .. db:errmsg())
26	end
27
28	db:exec("insert into test values('Balmer')")
29	print('last command changed ' .. db:changes() .. ' rows')
30
31	stmt = db:prepare("insert into test values(:name)")
32
33	print('statement has ' .. stmt:bind_parameter_count() .. ' parameters')
34	print('param 1 name: ' .. stmt:bind_parameter_name(1))
35	print('param name is at index ' .. stmt:bind_parameter_index(':name'))
36
37	stmt:bind(1, 'Hardmeier')
38	stmt:step()
39	stmt:reset()
40	stmt:bind(1, 'Keller')
41	stmt:step()
42	stmt:finalize()
43
44	s2 = db:prepare('select name from test')
45
46	while s2:step() == sqlite.ROW do
47		print('name = ' .. s2:column(1))
48	end
49	s2:finalize()
50
51	stmt = db:prepare('drop table test')
52	stmt:step()
53	stmt:finalize()
54	db:close()
55end
56
57print('shutdown sqlite')
58sqlite.shutdown()
59