修改函数报错返回处理,并增加新增用户的写入数据库接口数据函数进行数据库的信息写入
This commit is contained in:
parent
9c564c275a
commit
9fdf745a3a
|
|
@ -17,7 +17,7 @@ local function get_con(cfg)
|
|||
---- 连接到数据库
|
||||
local ok, err = conn:connect()
|
||||
if not ok then
|
||||
error("Connection failed: " .. err)
|
||||
print("Connection failed: " .. err)
|
||||
code = 0x000002
|
||||
end
|
||||
--ngx.say("Connection success")
|
||||
|
|
@ -35,8 +35,8 @@ function _M.getAllUser()
|
|||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
error("Query failed: " .. err)
|
||||
code = 0x000003
|
||||
print("get all users Query failed: "..sql)
|
||||
return 0x000003,res
|
||||
end
|
||||
--整理数据库结果返回值
|
||||
--for _, row in ipairs(res) do
|
||||
|
|
@ -60,8 +60,8 @@ function _M.getUser(id)
|
|||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
error("Query failed: " .. err)
|
||||
code = 0x000003
|
||||
print("Query failed: "..sql)
|
||||
return 0x000003,res
|
||||
end
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
|
|
@ -78,14 +78,31 @@ function _M.addUser(jsonData)
|
|||
if success == false then
|
||||
return 0x000001,res
|
||||
end
|
||||
--解析json中的键和数据值
|
||||
local keys = ""
|
||||
local values = ""
|
||||
for key, value in pairs(result) do
|
||||
keys = keys..key
|
||||
local val = type(value)
|
||||
if val == "string" then
|
||||
values = values.."\'"..value.."\'"
|
||||
else
|
||||
values = values..value
|
||||
end
|
||||
keys = keys..","
|
||||
values = values..","
|
||||
end
|
||||
local newKeys = keys:sub(1, #keys -1)
|
||||
local newValues = values:sub(1, #values -1)
|
||||
--组装sql语句
|
||||
local sql = "select * from \"T_Users\""
|
||||
local sql = string.format("insert into \"T_Users\"(%s)values(%s)", newKeys, newValues)
|
||||
--ngx.say(sql)
|
||||
--获取数据库连接
|
||||
local code,conn = get_con(dbconf.postgres)
|
||||
--执行数据库操作
|
||||
res = conn:query(sql)
|
||||
if not res then
|
||||
error("Query failed: " .. err)
|
||||
print("adduser sql failed: "..sql)
|
||||
return 0x000003,res
|
||||
end
|
||||
--关闭数据库
|
||||
|
|
@ -102,8 +119,8 @@ function _M.delete_user(id)
|
|||
--执行数据库操作
|
||||
local res = conn:query(sql)
|
||||
if not res then
|
||||
error("exec sql failed: " .. err)
|
||||
code = 0x000003
|
||||
print("delete exec sql failed: "..sql)
|
||||
return 0x000003,res
|
||||
end
|
||||
--关闭数据库
|
||||
conn:disconnect()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user