sqlite.lua revision 1.1
1-- $NetBSD: sqlite.lua,v 1.1 2011/10/15 12:58:43 mbalmer Exp $
2
3require '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',
17    sqlite.OPEN_READWRITE + sqlite.OPEN_CREATE)
18
19if state ~= sqlite.OK then
20	print('db open failed')
21else
22	err = db:exec('create table test (name varchar(32))')
23
24	if err ~= sqlite.OK then
25		print('table creation failed')
26		print('error code ' .. db:errcode() .. ' msg ' .. db:errmsg())
27	end
28
29	db:exec("insert into test values('Balmer')")
30	print('last command changed ' .. db:changes() .. ' rows')
31
32	stmt = db:prepare("insert into test values(:name)")
33
34	print('statement has ' .. stmt:bind_parameter_count() .. ' parameters')
35	print('param 1 name: ' .. stmt:bind_parameter_name(1))
36	print('param name is at index ' .. stmt:bind_parameter_index('name'))
37
38	stmt:bind(1, 'Hardmeier')
39	stmt:step()
40	stmt:reset()
41	stmt:bind(1, 'Keller')
42	stmt:step()
43	stmt:finalize()
44
45	s2 = db:prepare('select name from test')
46
47	while s2:step() == sqlite.ROW do
48		print('name = ' .. s2:column(1))
49	end
50	s2:finalize()
51
52	stmt = db:prepare('drop table testx')
53	stmt:step()
54	stmt:finalize()
55	db:close()
56end
57
58print('shutdown sqlite')
59sqlite.shutdown()
60
61