-- loaddriver
require"luasql.mysql"
-- createenvironmentobject
env = assert (luasql.mysql())
-- connecttodatasource
con = assert (env:connect("database", "usr", "password", "192.168.xx.xxx", 3306))
-- resetourtable
res = con:execute"DROP TABLE people" --建立新表people
res = assert (con:execute[[
CREATETABLEpeople(
namevarchar(50),
emailvarchar(50)
)
]])
-- addafewelements
list = {
{ name="Jose das Couves", email="jose@couves.com", },
{ name="Manoel Joaquim", email="manoel.joaquim@cafundo.com", },
{ name="Maria das Dores", email="maria@dores.com", },
}
fori, pinpairs (list) do --加入数据到people表
res = assert (con:execute(string.format([[
INSERTINTOpeople
VALUES ('%s', '%s')]], p.name, p.email)
))
end
-- retrieveacursor
cur = assert (con:execute"SELECT name, email from people") --获取数据
-- printallrows
row = cur:fetch ({}, "a") -- therowswillbeindexedbyfieldnames --显示出来
while row do
print(string.format("Name: %s, E-mail: %s", row.name, row.email))
row = cur:fetch (row, "a") -- reusingthetableofresults
end
-- closeeverything
cur:close()
con:close()
env:close()
阅读(1326) | 评论(0) | 转发(0) |