我的博客小站

LuaSQL

LuaSQL:http://www.keplerproject.org/luasql/

使用已有的库LuaSQL,示例程序如下: 

-- 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()

posted on 2011-08-30 16:23  BIGSING  阅读(776)  评论(0编辑  收藏  举报

导航

我的博客小站