当前位置: 首页 > news >正文

在Lua用luasql-sqlite3库访问SQLite数据库

用luarocks包管理器安装,注意库名是luasql-sqlite3,另一个相近的库luasql-sqlite会报缺少sqlite.h头文件错误。

luarocks install luasql-sqlite
Installing https://luarocks.org/luasql-sqlite-2.6.0-3.src.rockError: Could not find header file for SQLITENo file sqlite.h in /usr/local/includeNo file sqlite.h in /usr/includeNo file sqlite.h in /include
You may have to install SQLITE in your system and/or pass SQLITE_DIR or SQLITE_INCDIR to the luarocks command.
Example: luarocks install luasql-sqlite SQLITE_DIR=/usr/localwhereis sqlite.h
sqlite.h:
whereis sqlite3.h
sqlite3.h: /usr/include/sqlite3.h

安装,卡在克隆阶段,从现象看是连接github超时

luarocks install luasql-sqlite3
Installing https://luarocks.org/luasql-sqlite3-2.7.0-1.rockspec
Cloning into 'luasql'...
^C
Error: Failed cloning git repository.

手工下载包规格脚本

wget  https://luarocks.org/luasql-sqlite3-2.7.0-1.rockspec
--2025-10-29 12:35:35--  https://luarocks.org/luasql-sqlite3-2.7.0-1.rockspec
Resolving luarocks.org (luarocks.org)... 45.33.61.132
Connecting to luarocks.org (luarocks.org)|45.33.61.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 894 [text/x-rockspec]
Saving to: 'luasql-sqlite3-2.7.0-1.rockspec'luasql-sqlite3-2.7.0-1.rocksp 100%[=================================================>]     894  --.-KB/s    in 0s2025-10-29 12:35:36 (2.73 MB/s) - 'luasql-sqlite3-2.7.0-1.rockspec' saved [894/894]

内容如下

package = "LuaSQL-SQLite3"
version = "2.7.0-1"
source = {url = "git+https://github.com/lunarmodules/luasql.git",branch = "2.7.0",
}
description = {summary = "Database connectivity for Lua (SQLite3 driver)",detailed = [[LuaSQL is a simple interface from Lua to a DBMS. It enables aLua program to connect to databases, execute arbitrary SQL statementsand retrieve results in a row-by-row cursor fashion.]],license = "MIT/X11",homepage = "https://lunarmodules.github.io/luasql/"
}
dependencies = {"lua >= 5.1"
}
external_dependencies = {SQLITE = {header = "sqlite3.h"}
}
build = {type = "builtin",modules = {["luasql.sqlite3"] = {sources = { "src/luasql.c", "src/ls_sqlite3.c" },libraries = { "sqlite3" },incdirs = { "$(SQLITE_INCDIR)" },libdirs = { "$(SQLITE_LIBDIR)" }}}
}

将url改为能连上的地址比如url = "git+https://bgithub.xyz/lunarmodules/luasql.git",重新安装这个脚本,就能顺利克隆继续编译了。

luarocks install luasql-sqlite3-2.7.0-1.rockspec
Cloning into 'luasql'...
remote: Enumerating objects: 3263, done.
remote: Counting objects: 100% (138/138), done.
remote: Compressing objects: 100% (88/88), done.
remote: Total 3263 (delta 78), reused 53 (delta 50), pack-reused 3125 (from 3)
Receiving objects: 100% (3263/3263), 951.33 KiB | 243.00 KiB/s, done.
Resolving deltas: 100% (1998/1998), done.
Note: switching to 'bbd459e150e3fcc328aac1d794d6fdf62f1ce9e4'.You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:git switch -c <new-branch-name>Or undo this operation with:git switch -Turn off this advice by setting config variable advice.detachedHead to falseluasql-sqlite3 2.7.0-1 depends on lua >= 5.1 (5.4-1 provided by VM: success)
gcc -O2 -fPIC -I/usr/local/include -c src/luasql.c -o src/luasql.o -I/usr/include
gcc -O2 -fPIC -I/usr/local/include -c src/ls_sqlite3.c -o src/ls_sqlite3.o -I/usr/include
gcc  -shared -o /tmp/luarocks_build-LuaSQL-SQLite3-2.7.0-1-9198867/luasql/sqlite3.so src/luasql.o src/ls_sqlite3.o -L/usr/lib/x86_64-linux-gnu -Wl,-rpath,/usr/lib/x86_64-linux-gnu -lsqlite3
luasql-sqlite3 2.7.0-1 is now installed in /usr/local (license: MIT/X11)

使用
luasql-sqlite3没有专门的示例,在http://lunarmodules.github.io/luasql/examples.html找到一个postgreSQL示例,将相关的数据库字符串改为SQLite3就能执行了。

-- load driver
local driver = require "luasql.postgres"  --修改为local driver = require "luasql.sqlite3" 
-- create environment object
env = assert (driver.postgres()) --修改为env = assert (driver.sqlite3())
-- connect to data source
con = assert (env:connect("luasql-test"))
-- reset our table
res = con:execute"DROP TABLE people"
res = assert (con:execute[[CREATE TABLE people(name  varchar(50),email varchar(50))
]])
-- add a few elements
list = {{ name="Jose das Couves", email="jose@couves.com", },{ name="Manoel Joaquim", email="manoel.joaquim@cafundo.com", },{ name="Maria das Dores", email="maria@dores.com", },
}
for i, p in pairs (list) dores = assert (con:execute(string.format([[INSERT INTO peopleVALUES ('%s', '%s')]], p.name, p.email)))
end
-- retrieve a cursor
cur = assert (con:execute"SELECT name, email from people")
-- print all rows, the rows will be indexed by field names
row = cur:fetch ({}, "a")
while row doprint(string.format("Name: %s, E-mail: %s", row.name, row.email))-- reusing the table of resultsrow = cur:fetch (row, "a")
end
-- close everything
cur:close() -- already closed because all the result set was consumed
con:close()
env:close()

执行,成功地写入并读取出了数据。

lua sqli2.lua
Name: Jose das Couves, E-mail: jose@couves.com
Name: Manoel Joaquim, E-mail: manoel.joaquim@cafundo.com
Name: Maria das Dores, E-mail: maria@dores.com
http://www.dtcms.com/a/548611.html

相关文章:

  • 代码随想录第51 52天 | 图论-岛屿问题汇总
  • 分布式存储:Ceph、GlusterFS、MinIO架构与部署
  • 机械外协加工网最新订单移动网站如何优化排名
  • 11 种方法解决小米/米手机无法通过 USB 连接电脑的问题
  • Ubuntu:设置程序开机自启动
  • 化妆品品牌网站如何做wordpress pdf文章
  • vue 网站导航栏
  • 如何提高 IPA 安全性 多工具组合打造可复用的 iOS 加固与反编译防护体系(IPA 安全 iOS 加固 无源码混淆 Ipa Guard 实战)
  • 上海市工程建设交易中心网站深圳公司广告片制作
  • FreeRTOS 学习:(三)HAL库、标准库 和 FreeRTOS 的关联性,简述
  • 使用 Tauri + Rust 构建跨平台桌面应用:前端技术的新边界
  • 如何录屏?【图文详解】免费录屏软件?电脑如何录屏?电脑怎么录屏?
  • 深入Rust:Box、Rc、Arc智能指针机制解析与实践指南
  • 【项目实践】公寓租赁项目(十):基于SpringBoot登录管理接口开发
  • Java1030 abstract 继承
  • 第六部分:VTK进阶(第180章 重采样与插值)
  • 聊城做网站推广哪家好android sdk
  • 时间序列早期分类中的置信度累积问题:从ECE-C到时序依赖建模
  • Rust + WebAssembly + Svelte + TypeScript + Zod 全栈开发深度指南
  • 【android bluetooth 协议分析 18】【PBAP详解 2】【车机为何不显示电话号码为空的联系人信息】
  • MacPro2012升级Monterey终极解决方案
  • 软件项目管理工具
  • Actix Web适合什么类型的Web应用?可以部署 Java 或 .NET 的应用程序?
  • Android PDF 操作 - AndroidPdfViewer 弹出框显示 PDF
  • 做新零售这些注册网站和找货源6内蒙古网站建设流程
  • 【Linux篇】进程间通信 - 匿名管道
  • Java Stream Collectors主要 API 详解
  • Mac如何安装mysql和完全卸载mysql
  • 【Docker】【03.使用docker搭建ubuntu20.04 Qt5.12 开发环境】
  • uni-app 上架 iOS 应用全流程 从云打包到开心上架(Appuploader)免 Mac 上传发布指南