首页 技术 正文
技术 2022年11月13日
0 收藏 732 点赞 4,016 浏览 2780 个字
-- 演示将多条记录数据组合成一条sql插入语句(for mysql)function getTpl0(tname)-- 获取表各个字段
local t = {
tpl_pack = {"packId","itemId","`group`","num","rate","rateType"},
}
for k, v in pairs(t) do
if tname == k then
return t[k]
end
end
end--tpl = {3813,10,0,2,0,1,1,0,350,5,220,6,0,0,0,0,154,0,0,0,210,80,29}
tpl9122 = {
-- "packId","itemId","`group`","num","rate","rateType"
{9122, 3294, '', 1, 1, 2},
{9122, 3295, '', 1, 1, 2},
{9122, 3296, '', 1, 1, 2},
{9122, 3297, '', 1, 1, 2},
{9122, 3298, '', 1, 1, 2},{9122, 9004, '', 2, 4, 2},
{9122, 117, '', 8, 4, 2},
{9122, 118, '', 8, 4, 2},
{9122, 119, '', 8, 4, 2},
{9122, 120, '', 8, 4, 2},
{9122, 322, '', 2, 4, 2},
{9122, 160, '', 5, 5, 2},
{9122, 327, '', 5, 5, 2},
{9122, 2900, '', 1, 6, 2},
{9122, 9101, '', 20, 7, 2},
{9122, 115, '', 15, 10, 2},
{9122, 114, '', 15, 12, 2},
{9122, 112, '', 15, 13, 2},
{9122, 113, '', 15, 13, 2},
}tpl9123 = {
-- "packId","itemId","`group`","num","rate","rateType"
{9123, 3299, '', 1, 1, 2},
{9123, 3300, '', 1, 1, 2},
{9123, 3301, '', 1, 1, 2},
{9123, 3302, '', 1, 1, 2},
{9123, 3303, '', 1, 1, 2},{9123, 9004, '', 2, 4, 2},
{9123, 117, '', 8, 4, 2},
{9123, 118, '', 8, 4, 2},
{9123, 119, '', 8, 4, 2},
{9123, 120, '', 8, 4, 2},
{9123, 322, '', 2, 4, 2},
{9123, 160, '', 5, 5, 2},
{9123, 327, '', 5, 5, 2},
{9123, 2900, '', 1, 6, 2},
{9123, 9101, '', 20, 7, 2},
{9123, 115, '', 15, 10, 2},
{9123, 114, '', 15, 12, 2},
{9123, 112, '', 15, 13, 2},
{9123, 113, '', 15, 13, 2},
}function createInsertSql(tname, tpl)
local tpl0 = getTpl0(tname)-- 获取表各个字段
local ret = {}-- 插入记录sqltable.insert(ret, string.format("insert into `%s`(", tname))
for k, v in pairs(tpl0) do
if k > 1 then
table.insert(ret, ",")
end
table.insert(ret, v)
end
table.insert(ret, ") values ")for k, v in pairs(tpl) do
if k > 1 then
table.insert(ret, ",")
end
table.insert(ret, "(")
for k2, v2 in pairs(v) do
if k2 > 1 then
table.insert(ret, ",")
end
if type(v2) == "string" then
table.insert(ret, string.format("'%s'", v2))
else
table.insert(ret, v2)
end
end
table.insert(ret, ")")
end
table.insert(ret, ";")local result = table.concat(ret)-- 最终的sql语句
print(result)
print()
end
createInsertSql("tpl_pack", tpl9122)
createInsertSql("tpl_pack", tpl9123)

最终的执行结果如下:

[zcm@lua 6]$lua t1.lua
insert into `tpl_pack`(packId,itemId,`group`,num,rate,rateType) values (9122,3294,'',1,1,2),(9122,3295,'',1,1,2),(9122,3296,'',1,1,2),(9122,3297,'',1,1,2),(9122,3298,'',1,1,2),(9122,9004,'',2,4,2),(9122,117,'',8,4,2),(9122,118,'',8,4,2),(9122,119,'',8,4,2),(9122,120,'',8,4,2),(9122,322,'',2,4,2),(9122,160,'',5,5,2),(9122,327,'',5,5,2),(9122,2900,'',1,6,2),(9122,9101,'',20,7,2),(9122,115,'',15,10,2),(9122,114,'',15,12,2),(9122,112,'',15,13,2),(9122,113,'',15,13,2);insert into `tpl_pack`(packId,itemId,`group`,num,rate,rateType) values (9123,3299,'',1,1,2),(9123,3300,'',1,1,2),(9123,3301,'',1,1,2),(9123,3302,'',1,1,2),(9123,3303,'',1,1,2),(9123,9004,'',2,4,2),(9123,117,'',8,4,2),(9123,118,'',8,4,2),(9123,119,'',8,4,2),(9123,120,'',8,4,2),(9123,322,'',2,4,2),(9123,160,'',5,5,2),(9123,327,'',5,5,2),(9123,2900,'',1,6,2),(9123,9101,'',20,7,2),(9123,115,'',15,10,2),(9123,114,'',15,12,2),(9123,112,'',15,13,2),(9123,113,'',15,13,2);

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854