require "luasql.mysql"_lua5.1 luarockts mysql

1.

安装 lua5.1 luarocks

apt-get install luarocks

这样会一并安装所有涉及

建议先安装lua5.1 再安装luarocks

2.

环境: ubuntu 已安装lua,未安装mysql

解决方案

一、先安装mysql

Ubuntu上安装MySQL非常简单只需要几条命令就可以完成。

1. sudo apt-get install mysql-server

2. apt-get isntall mysql-client

3. sudo apt-get install libmysqlclient-dev

//不安装的话会缺少MySQL.h

参考链接

http://www.linuxidc.com/Linux/2016-07/133128.htm

二 安装luasql-mysql

luarocks install luasql-mysql

参考链接

http://www.runoob.com/lua/lua-database-access.html

三 安装luasql-mysql

出错

Error: Could not find header file for MYSQL

No file mysql.h in /usr/local/mysql

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

解决方法:

在终端执行whereis mysql 找到mysql 的include路径

luarocks install luasql-mysql

MYSQL_INCDIR=/usr/include/mysql

注释:配置自己查到的mysql.h 所在路径

3.

luasql=require

"luasql.mysql"

--创建环境对象

env = luasql.mysql()

--连接数据库

conn =

env:connect("数据库名","用户名","密码","IP地址",端口)

--设置数据库的编码格式

conn:execute"SET NAMES

UTF8"

--执行数据库操作

cur = conn:execute("select * from

role")

row = cur:fetch({},"a")

--文件对象的创建

file =

io.open("role.txt","w+");

while row do

var = string.format("%d %s\n", row.id,

row.name)

print(var)

file:write(var)

row = cur:fetch(row,"a")

end

file:close()

--关闭文件对象

conn:close()

--关闭数据库连接

env:close()  --关闭数据库环境

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值