我的博客小站

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   BIGSING  阅读(784)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

我的博客小站
点击右上角即可分享
微信分享提示