lua之mysql编程

环境搭建

#mysql 安装
sudo apt-get install mysql-server mysql-client     
# lua mysql 开发驱动安装
sudo apt-get install lua-sql-msyql-dev

使用案例

-- 打开相关对象
local mysqlDriver = require "luasql.mysql"   -- 获取驱动对象driver
local env = mysqlDriver.mysql()              -- 构建环境对象
local conn = env:connect("test", "root", "******")  -- mysql 接连对象
-- 通过conn:execute执行sql语句,当执行非查询语句返回影响行和错误字符串, 当执行查询语句时返回光标对象和错误字符串
-- 执行非查询语句
local status, err = conn:execute([[insert into user values(NULL,"xuejian", "F")]])
-- 执行查询语句
local cursor, err = conn:execute([[select * from user]])
local row = cursor:fetch({}, 'a')            -- 第一个参数放个表对象,用于获取值, 第二个参数表示存值方式 a -- 字典 n -- 数组
while row do
    row = cursor:fetch(row, 'a')             -- 当取完记录再取,cursor对象自动关闭并放回nil
end 
-- 关闭相关对象
cursor:close()
conn:close()
env:close()

参考链接

luasql 官方教程

转载于:https://www.cnblogs.com/--xiaoyao--/p/6143365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值