NetBSD/share/examples/lua/sqlite.lua
kamil 8ad8b0676c Correct mistakes in the sqlite.lua example
Changes:
- The open flag: sqlite.OPEN_CREATE will open the DB for reading and
  writing, adding sqlite.OPEN_READWRITE to sqlite.OPEN_CREATE will cause
  the DB to not be created and prevent the script from continuing
- When using stmt:bind_parameter_index() the parameter needs to be
  prefixed with ':' if that was used in the prepared statement,
  otherwise the incorrect index of 0 is returned.
- The drop table statement has an "x" appended to the table name, looks
  like a typo.

Patch by Travis Paul

Closes PR misc/50493
2015-12-08 23:04:40 +00:00

59 lines
1.4 KiB
Lua

-- $NetBSD: sqlite.lua,v 1.3 2015/12/08 23:04:40 kamil Exp $
local sqlite = require 'sqlite'
print(sqlite._VERSION .. ' - ' .. sqlite._DESCRIPTION)
print(sqlite._COPYRIGHT)
print()
print('initialize sqlite')
sqlite.initialize()
print('this is sqlite ' .. sqlite.libversion() .. ' (' ..
sqlite.libversion_number() .. ')')
print('sourceid ' .. sqlite.sourceid())
db, state = sqlite.open('/tmp/db.sqlite', sqlite.OPEN_CREATE)
if state ~= sqlite.OK then
print('db open failed')
else
err = db:exec('create table test (name varchar(32))')
if err ~= sqlite.OK then
print('table creation failed')
print('error code ' .. db:errcode() .. ' msg ' .. db:errmsg())
end
db:exec("insert into test values('Balmer')")
print('last command changed ' .. db:changes() .. ' rows')
stmt = db:prepare("insert into test values(:name)")
print('statement has ' .. stmt:bind_parameter_count() .. ' parameters')
print('param 1 name: ' .. stmt:bind_parameter_name(1))
print('param name is at index ' .. stmt:bind_parameter_index(':name'))
stmt:bind(1, 'Hardmeier')
stmt:step()
stmt:reset()
stmt:bind(1, 'Keller')
stmt:step()
stmt:finalize()
s2 = db:prepare('select name from test')
while s2:step() == sqlite.ROW do
print('name = ' .. s2:column(1))
end
s2:finalize()
stmt = db:prepare('drop table test')
stmt:step()
stmt:finalize()
db:close()
end
print('shutdown sqlite')
sqlite.shutdown()