Fork me on GitHub

lua操作json,mysql,redis等

==========================example for lua json=======================
local cjson = require("cjson")

local str = '["a", "b", "c"]'
local j = cjson.decode(str)
for i,v in ipairs(j) do
print(v)
end

str = '{"A":1, "B":2}'
j = cjson.decode(str)
for k,v in pairs(j) do
print(k..":"..v)
end
j['C']='c'
new_str = cjson.encode(j)
print(new_str)

==========================example for lua mysql=======================
mysql.lua
require "luasql.mysql"
env = assert(luasql.mysql())

conn = assert(env:connect("fsdb","root","123456","127.0.0.1",3306))

conn:execute"SET NAMES utf8"
function rows (connection, sql_statement)
local cursor = assert (connection:execute (sql_statement))
return function ()
return cursor:fetch()
end
end

for did, status in rows(conn ,"SELECT driverid,status from fs_driver_products") do
print(string.format("%s %s",name,email))
end

conn:close()
env:close()

lua操作mysql的库文件

http://luaforge.net/projects/luasql/
支持mysql sqlite oracle(oci)postgres和odbc

lua操作redis的库文件
https://github.com/gitforks/redis-lua

需要依赖luasocket库文件。
http://luaforge.net/projects/luasocket/
此库文件里面有lua对网络的一些操作库,如http smtp ftp redis mime等lua实现。

Lua加载lua文件时,需要设置 package.path
如:package.path = "依赖的lua文件所在的目录,支持正则方式加载" .. package.path
package.path = "../deps/lua/?.lua" .. package.path
Lua加载so动态库,需要设置 package.cpath
package.cpath = "../deps/lua/help.so" .. package.cpath

posted @ 2015-12-23 18:49  Mr.YF  阅读(2234)  评论(0编辑  收藏  举报