-
Notifications
You must be signed in to change notification settings - Fork 18
/
mysql_fun.lua
139 lines (130 loc) · 3.95 KB
/
mysql_fun.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
--[[-----------------------------------------------------------------------
* | Copyright (C) Shaobo Wan (Tinywan)
* | Github: https://github.com/Tinywan
* | Blog: http://www.cnblogs.com/Tinywan
* |------------------------------------------------------------------------
* | Date: 2017/6/02 22:29
* | Function: msyql add select udpate delete option
* |------------------------------------------------------------------------
--]]
local mysql = require 'resty.mysql'
local cjson = require 'cjson'
local db_config = {
host = '127.0.0.1',
port = 3306,
database = 'lua',
user = 'root',
password = '123456',
max_packet_size = 1024 * 1024,
timeout = 1000,
table = 'tb_ngx_test'
}
local db, err = mysql:new()
if not db then
ngx.log(ngx_ERR, "failed to instantiate mysql: ", err) -- 未安装mysql客户端
return
end
local ok, err, errcode, sqlstate = db:connect({
host = db_config.host,
port = db_config.port,
database = db_config.database,
user = db_config.user,
password = db_config.password
})
if not ok then
ngx.log(ngx_ERR, "failed to connect: ", err, ": ", errcode, " ", sqlstate) -- mysql连接不上
return
end
-- 添加
-- data = { name = "XIDaDa", address = "HeilongJiang", age = "66" }
-- local result = add(data.name,data.address,data.age)
-- ngx.print(cjson.encode(result))
function add(name, address, age)
local res = {}
if name ~= nil then
local sql = "INSERT INTO tb_ngx_test (name,address,age) VALUES (\'" .. name .. "\',\'" .. address .. "\'," .. age .. ")";
local data, err, errcode, sqlstate = db:query(sql)
if not data then
res.error_code = 501
res.message = 'add failed ' .. '[err]:' .. err .. ',[sql]:' .. sql
res.result = nil
else
res.error_code = 200
res.message = 'add successfully'
res.result = res.insert_id
end
else
res.error_code = 501
res.message = 'parameter error'
res.result = nil
end
return res
end
-- 查询
-- local result = select(3)
-- ngx.print(cjson.encode(result))
function select(id)
local res = {}
if id ~= nil then
local data, err, errno, sqlstate = db:query('SELECT * FROM ' .. db_config.table .. ' WHERE id=' .. id .. ' LIMIT 1', 1)
if data ~= nil then
res.error_code = 200
res.message = 'search successfully'
res.result = data[1]
else
res.error_code = 502
res.message = 'no data'
res.result = data
end
else
res.error_code = 501
res.message = 'parameter error'
res.result = nil
end
return res
end
-- 修改
-- local result = update(3,"TinTinAIAI")
-- ngx.print(cjson.encode(result))
function update(id, name)
local res = {}
if id ~= nil and name ~= nil then
local data, err, errno, sqlstate = db:query('UPDATE ' .. db_config.table .. ' SET `name` = "' .. name .. '" WHERE id=' .. id)
if not data or data.affected_rows < 1 then
res.error_code = 504
res.message = 'fail to update'
else
res.error_code = 200
res.message = 'successfully modified'
end
else
res.error_code = 501
res.message = 'parameter error'
res.result = nil
end
return res
end
-- 删除操作
--local result = delete(3)
--ngx.print(cjson.encode(result))
function delete(id)
local res = {}
if id ~= nil then
local data, err, errno, sqlstate = db:query('DELETE FROM ' .. db_config.table .. ' WHERE id=' .. id)
if not data or data.affected_rows < 1 then
res.error_code = 504
res.message = 'failed to delete'
else
res.error_code = 200
res.message = 'successfully deleted'
end
else
res.error_code = 501
res.message = 'parameter error'
res.result = nil
end
return res
end
function add(a,b)
return a+b
end