You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2 KiB
Lua

local _M={}
function _M.update(tablename,row,conds)
local vals = {}
for k,v in pairs(row) do
if type(v) =='string' then
table.insert(vals,string.format("%s='%s'",k,v))
elseif type(v) =='number' then
table.insert(vals,string.format("%s=%s",k,tostring(v)))
else
assert(false,'not support')
end
end
local strvals = table.concat(vals,",")
local fmt = "update `%s` set %s where %s"
local sql = string.format(fmt,tablename,strvals,conds)
return sql
end
function _M.insert(tablename,row)
local cols = {}
local vals = {}
for k,v in pairs(row) do
table.insert(cols,string.format("`%s`",k))
if type(v) =='string' then
table.insert(vals,string.format("'%s'",v))
elseif type(v) =='number' then
table.insert(vals,string.format("%s",tostring(v)))
else
assert(false,'not support')
end
end
local strcols = table.concat(cols,",")
local strvals = table.concat(vals,",")
local fmt = "insert into `%s` (%s) values(%s)"
local sql = string.format(fmt,tablename,strcols,strvals)
return sql
end
return _M