English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dieser Artikel stellt den Lua-Datenbank-Operations-Bibliothek vor:LuaSQL. Es ist Open Source und unterstützt Datenbanken wie: ODBC, ADO, Oracle, MySQL, SQLite und PostgreSQL.
Dieser Artikel stellt die Datenbankverbindung von MySQL vor.
LuaSQL kann verwendet werden LuaRocks Um entsprechend benötigte Datenbanktreiber zu installieren.
LuaRocks Installationsmethoden:
$ wget http://luarocks.org/Releases/luarocks-2.2.1.tar.gz $ tar zxpf luarocks-2.2.1.tar.gz $ cd luarocks-2.2.1 $ ./konfigurieren; sudo make bootstrap $ sudo luarocks install luasocket $ lua Lua 5.3.0 Urheberrecht (C) 1994-2015 Lua.org, PUC-Rio > require "socket"
LuaRocks unter Windows installieren:https://github.com/keplerproject/luarocks/wiki/Installation-Anweisungen-for-Windows
安装不同数据库驱动:
luarocks install luasql-sqlite3 luarocks install luasql-postgres luarocks install luasql-mysql luarocks install luasql-sqlite luarocks install luasql-odbc
你也可以使用源码安装方式,Lua Github 源码地址:https://github.com/keplerproject/luasql
Lua 连接MySql 数据库:
require "luasql.mysql" --5.2 版本之后,require 不再定义全局变量,需要保存其返回值。 --需要写成: --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() --关闭数据库环境