-
Notifications
You must be signed in to change notification settings - Fork 9
/
lua-mysql.lua
52 lines (39 loc) · 1012 Bytes
/
lua-mysql.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
--测试连接mysql,获取数据
local function close_db(db)
if not db then
return
end
db:close()
end
local mysql = require("resty.mysql")
local json = require("dkjson")
local db, err = mysql:new()
if not db then
ngx.say("error : ", err)
return
end
db:set_timeout(1000)
local props = {
host = "192.168.1.104",
port = 3306,
database = "sonar",
user = "root",
password = "root"
}
local res, err, errno, sqlstate = db:connect(props)
if not res then
ngx.say("connect error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
return close_db(db)
end
local select_sql = "select * from dashboards"
res, err, errno, sqlstate = db:query(select_sql)
if not res then
ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
return close_db(db)
end
for i, row in ipairs(res) do
for name, value in pairs(row) do
ngx.say("select row =", i, " : ", name, " = ", value, "<br/>")
end
end
close_db(db)