1. 用Brew安装Lua
brew install lua
2. 安装luarocks
luarocks 是 Lua 的包管理工具
brew install luarocks
3. 安装 luasocket 依赖,远程调试需要用到
luarocks install luasocket
Installing https://luarocks.org/luasocket-3.0rc1-2.src.rock
env MACOSX_DEPLOYMENT_TARGET=...
luasocket 3.0rc1-2 is now installed in /usr/local (license: MIT)
4. 用IDE开发Lua安装lua插件
我安装的Lua插件
运行一个demo
结果:
5. 用IDE调试Lua代码
IDE不能本地调试,只能用远程调试的方式。
使用 MobDebug 来进行远程调试,可以参考主页说明。
下载项目,在项目的src目录下运行语句
lua -e "require('mobdebug').listen()"
Lua Remote Debugger
Run the program you wish to debug
在ide中设置断点,并Debug
6. 安装数据库依赖包
6.1 安装 luasql-mysql
luarocks install luasql-mysql
Warning: falling back to curl - install luasec to get native HTTPS support
Installing https://luarocks.org/luasql-mysql-2.4.0-1.src.rock
Error: Could not find header file for MYSQL
No file mysql.h in /usr/local/include
No file mysql.h in /usr/include
No file mysql.h in /include
You may have to install MYSQL in your system and/or pass MYSQL_DIR or MYSQL_INCDIR to the luarocks command.
Example: luarocks install luasql-mysql MYSQL_DIR=/usr/local
安装失败,缺少依赖的 xx.h
头文件,把需要的头文件放在自己的自定义的环境路径中:
pwd
/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15/include
ls
errmsg.h mariadb_ctype.h mysql.h
ma_list.h mariadb_stmt.h
mariadb_com.h mariadb_version.h
此时自定义路径安装 luasql-mysql
luarocks install luasql-mysql MYSQL_DIR=/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15
Warning: falling back to curl - install luasec to get native HTTPS support
Installing https://luarocks.org/luasql-mysql-2.4.0-1.src.rock
env MACOSX_DEPLOYMENT_TARGET=10.8 gcc -O2 -fPIC -I/usr/local/opt/lua/include/lua5.3 -c src/luasql.c -o src/luasql.o -I/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15/include
env MACOSX_DEPLOYMENT_TARGET=10.8 gcc -O2 -fPIC -I/usr/local/opt/lua/include/lua5.3 -c src/ls_mysql.c -o src/ls_mysql.o -I/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15/include
env MACOSX_DEPLOYMENT_TARGET=10.8 gcc -bundle -undefined dynamic_lookup -all_load -o luasql/mysql.so src/luasql.o src/ls_mysql.o -L/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15/lib -Wl,-rpath,/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15/lib: -lmysqlclient
ld: warning: directory not found for option '-L/Users/jjz/workspace/lua-env/luasql/mysql-8.0.15/lib'
luasql-mysql 2.4.0-1 is now installed in /usr/local (license: MIT/X11)
测试一下:
luasql = require "luasql.mysql"
--创建环境对象
env = luasql.mysql*()*
--连接数据库
conn = env:connect*(*"dcm","root","","localhost",3306*)*
--设置数据库的编码格式
conn:execute"SET NAMES UTF8"
--执行数据库操作
cur = conn:execute*(*"select * from dcm_action"*)*
row = cur:fetch*({}*,"a"*)*
--文件对象的创建
file = io.open*(*"role.txt","w+"*)*;
**while**row *do*
var = string.format*(*"%s", row.id*)*
print*(*var*)*
file:write*(*var*)*
row = cur:fetch*(*row,"a"*)*
*end*
file:close*()*--关闭文件对象
conn:close*()*--关闭数据库连接
env:close*()*--关闭数据库环境
结果:
/usr/local/Cellar/lua/5.3.5_1/bin/lua mysqlDemo.lua
4cbROUp2nnxXA6Uk
4cbROVD2nnbdC2Uo
Process finished with exit code 0