在luasql的安装过程中不能成功编译,主要遇到两个问题

  1. 缺失mysql.h
    luasql脚本工具的安装

    在安装了mysql的前提下,还需要再安装libmysqlclient-dev,具体指令:sudo apt-get install libmysqlclient-dev

  2. 系统所用的lua版本及路径与luasql的不一致
    将config文件的lua路径配置成与系统的一致,具体路径查看,可以使用指令whereis或which
    luasql脚本工具的安装

    编译完成之后,模仿tests/example.lua写了一个test_mysql.lua 执行时发现有错误:
    luasql脚本工具的安装
    注意到红色框内的错误信息,将src目录下的mysql.so拷贝到上述目录下(luasql目录需要自己建立)
    这个我自身并没有遇见,参考网上有这个错误,拿来记录。
    之后再次执行test_mysql.lua 发现可以了,通过远程sqlyog查看到数据库内已经有了数据,附上测试代码:

-- load driver
local luasql = require "luasql.mysql"


-- create environment object
env = assert(luasql.mysql())
-- connect to data source
-- args: 数据库名 帐号 密码 IP 端口(IP及端口可以不用改)
conn = assert(env:connect("test","root","123","127.0.0.1",3306))

-- reset our table
res = conn:execute"DROP TABLE people"

res = assert(conn:execute[[
        CREATE TABLE people(
        name  varchar(50),
        email varchar(50)
        )
]])

-- add a few elements

list = {       
 		{ name="Jose das Couves", email="[email protected]", },    
     	{ name="Manoel Joaquim", email="[email protected]", },    
        { name="Maria das Dores", email="[email protected]",},
}

for i, p in pairs (list) do
	res = assert (conn:execute(string.format([[
		INSERT INTO people
		VALUES ('%s', '%s')]], p.name, p.email)
	 ))
end
-- retrieve a cursor

cur = assert (conn:execute"SELECT name, email from people")
-- print all rows

row = cur:fetch ({}, "a")       -- the rows will be indexed by field names

while row do
	print(string.format("Name: %s, E-mail: %s", row.name, row.email))
	row = cur:fetch (row, "a")    -- reusing the table of results
end
-- close everything

cur:close()
conn:close()
env:close()

执行命令:

lua test_mysql.lua

正常显示为:
luasql脚本工具的安装

相关文章:

  • 2022-12-23
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2021-09-09
  • 2021-09-27
猜你喜欢
  • 2022-01-20
  • 2021-12-03
  • 2021-07-02
  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
相关资源
相似解决方案