Lua + mysql 实战代码

复制代码
--[[
    luarocks lua语言的包管理器

    luasql  https://luarocks.org/

    brew install luarocks
    luarocks install luasql-mysql   注意此处,如果你是 mariadb,然后要求指定 MYSQL_DIR 参数的时候,
                                        千万不要指到 mariadb 的安装目录,而是要指到一个真正的mysql 安将目录
                                        那是不是不能用 mariadb 呢,也不是,要手动安装一遍

    luarocks install luasql-sqlite3
    luarocks install luasql-postgres
    luarocks install luasql-mysql
    luarocks install luasql-sqlite
    luarocks install luasql-odbc

]]--

local luasql = require("luasql.mysql");

client = luasql.mysql();

--创建连接
conn=client:connect("lua-test","root","12345678","127.0.0.1",3306); --数据库名,用户名,密码,IP,端口

if conn == nil then
    print("连接失败");
    os.exit();
end

--select
rs=conn:execute("select * from user_list order by id desc");

row = rs:fetch({},"a");

while row do

    print(row.id,row.name,row.age);

    row = rs:fetch(row,"a");

end

--insert
--rs=conn:execute("insert into user_list (name,age)values('def',456)");
--print(rs);

--update
rs=conn:execute("update user_list set name='DEF',age=789 where id<=4");
print(rs);

--delete
rs=conn:execute("delete from  user_list where id=4");
print(rs);

conn:close();
client:close();
相关推荐
FungLeo2 小时前
FengCMS · PHP+MySQL 升级实录 ·双数据库验证:MySQL8.4与MariaDB 10.11的兼容矩阵
数据库·mysql·php·工程实践·fengcms
星恒随风4 小时前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制
linux·服务器·开发语言·c++·人工智能
yingjie1105 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
Felven5 小时前
C. Make Them Equal
c语言·开发语言·前端
tryCbest6 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
风落青芜7 小时前
排序算法(四):归并排序、计数排序与算法总结
c语言·开发语言·数据结构·算法·排序算法
Resean02238 小时前
讲透Java(14)泛型的本质:类型擦除与桥接方法
java·开发语言
有个傻瓜10 小时前
手机号明明查不到,注册却提示“已存在“:MyBatis-Plus 逻辑删除与唯一索引冲突排错复盘与五种方案实测
mysql·mybatis-plus·故障排查·唯一索引·逻辑删除
FungLeo10 小时前
FengCMS · PHP+MySQL 升级实录 · 留言模块的六项问题:未审核进公开列表、旧回复覆盖、批量删除恢复
android·mysql·php·工程实践·fengcms
stormsha11 小时前
中文路径让 MySQL 起不来:Failed to find valid data directory 与 Illegal byte sequence
运维·数据库·mysql