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();
相关推荐
张洛闻Eren8 分钟前
MySQL 管理复制拓扑【MySQL第四课】
linux·运维·数据库·mysql
SomeB1oody39 分钟前
【RustyML入门】5.0. 模型评估
开发语言·后端·机器学习·rust·教程
Evand J1 小时前
【MATLAB例程,PDR11】二维平面下的PDR(行人航位推算)步态检测与EKF融合定位,附代码的下载链接
开发语言·matlab·平面·pdr·行人导航·步态检测
牛油果子哥q1 小时前
C++STL算法超全精讲:排序/查找/去重/遍历/最值/合并全套API、仿函数、Lambda适配、实战避坑
开发语言·c++·算法
Xeon_CC1 小时前
用C++编写屏幕准星
开发语言·c++
孤存5222 小时前
c语言动态内存管理
c语言·开发语言·算法
小王C语言2 小时前
MySQL 内置函数:日期函数、字符串函数、数学函数、其他函数
数据库·mysql
冰暮流星2 小时前
javascript之webstorage用法
开发语言·javascript·ecmascript
我一定会有钱2 小时前
打造完美工作流:Git 配置”一次推送,三端同步”(Gitee/GitCode/GitHub)
开发语言·windows·vscode·搜索引擎·gitlab·github·visual studio code
matlab代码2 小时前
基于matlab肤色人脸标记检测系统 GUI界面【源码53期】
开发语言·matlab·人脸标记