YouTip LogoYouTip

Lua Database Access

# Lua Database Access This article mainly introduces the Lua database operation library: (http://luaforge.net/projects/luasql/). It is open source and supports the following databases: ODBC, ADO, Oracle, MySQL, SQLite, and PostgreSQL. This article introduces the MySQL database connection. LuaSQL can be installed using (https://luarocks.org/), and you can install the required database drivers as needed. LuaRocks installation method: $ wget http://luarocks.org/releases/luarocks-2.2.1.tar.gz $ tar zxpf luarocks-2.2.1.tar.gz $ cd luarocks-2.2.1 $ ./configure; sudo make bootstrap $ sudo luarocks install luasocket $ lua Lua 5.3.0 Copyright (C)1994-2015 Lua.org, PUC-Rio > require "socket" Installing LuaRocks on Windows: [https://github.com/keplerproject/luarocks/wiki/Installation-instructions-for-Windows](https://github.com/keplerproject/luarocks/wiki/Installation-instructions-for-Windows) Installing different database drivers: luarocks install luasql-sqlite3 luarocks install luasql-postgres luarocks install luasql-mysql luarocks install luasql-sqlite luarocks install luasql-odbc You can also install from source. Lua Github source address: [https://github.com/keplerproject/luasql](https://github.com/keplerproject/luasql) Lua connecting to MySQL database: ## Example require"luasql.mysql" --Create environment object env = luasql.mysql() --Connect to database conn = env:connect("database_name","username","password","IP_address",port) --Set database encoding format conn:execute"SET NAMES UTF8" --Execute database operation cur = conn:execute("select * from role") row = cur:fetch({},"a") --Create file object file =io.open("role.txt","w+"); while row do var =string.format("%d %sn", row.id, row.name) print(var) file:write(var) row = cur:fetch(row,"a") end file:close()--Close file object conn:close()--Close database connection env:close()--Close database environment
← Python3 Add NumberAndroid Tutorial β†’