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();
相关推荐
Zhou1411366 分钟前
SpringSecurity_02_授权与高级功能
开发语言·python
知行产研23 分钟前
易控智驾:毛利率跃升17.2个百分点,矿山无人驾驶从“规模换增长“迈入“盈利拐点“
数据库·mysql
码匠许师傅40 分钟前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http
可涵不会debug1 小时前
第一次学 LangGraph:用一个快递案例搞懂 State、Node、Edge 和 compile
服务器·数据库·mysql
小小小小钰儿2 小时前
1.5-Python网络编程
开发语言·网络·python·web安全·计算机·网络安全
hiahiahia1232 小时前
实现完整 Tool Dispatcher
开发语言·前端
用户250458106092 小时前
数据库索引为什么会失效?从执行计划理解 SQL 性能优化
sql·mysql
AC赳赳老秦2 小时前
公开音频转写信息提取:OpenClaw 处理发布会与听证会文本并提取核心决策信息
大数据·开发语言·汇编·数据库·人工智能·deepseek·openclaw