-- 演示将多条记录数据组合成一条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 = {} -- 插入记录sql table.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);
 

[lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)的更多相关文章

  1. sqlserver中 多条数据合并成一条数据 (stuff 与 for xml path 连用)

    SQL 列转行,即多行合并成一条   需求:按照分组,将多条记录内容合并成一条,效果如下: 数据库示例: CREATE TABLE [t2]([NID] [bigint] NULL,[district ...

  2. mysql 查询一条记录的下一条和上一条记录

    如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from tab ...

  3. Slq怎么样获取首条记录和最后一条记录

    sql如何查询表的第一条记录和最后一条记录 方法一:使用top select TOP 1 * from apple;TOP 1 表示表apple中的第一条数据 select TOP 1 * from ...

  4. 错误:违反并发性: DeleteCommand 影响了预期 1 条记录中的 0 条

    在access的mdb数据库动态更新的过程中,遇到了DeleteCommand出现DBConcurrencyException异常,错误:违反并发性: DeleteCommand 影响了预期 1 条记 ...

  5. 违反并发性: UpdateCommand影响了预期 1 条记录中的 0 条 解决办法

    本文转载:http://www.cnblogs.com/litianfei/archive/2007/08/16/858866.html UpdateCommand和DeleteCommand出现DB ...

  6. 快速将一个表的数据生成SQL插入语句

    将一个表中的数据生成SQL插入语句,方便系统快速初始化,在数据库中执行创建以下过程就可以了. ) Drop Procedure GenerateData go CREATE PROCEDURE Gen ...

  7. 从mysql数据库删除重复记录只保留其中一条

    这两天做了一个调用第三方接口的小程序,因为是实时更新数据,所以请求接口的频率就很高,这样有时会出现往数据库插入重复的数据,对数据库造成压力也不方便管理,因为要通过原生sql语句,解决数据库的去重问题. ...

  8. MySQL查询上一条记录和下一条记录

    如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from tab ...

  9. FPC报价模块配置 UpdateCommand影响了预期 1 条记录中的 0 条 解决办法

    今天在增加P4厂 FPC报价模块配置,增加刚挠信息节点,在保存时报错:UpdateCommand影响了预期 1 条记录中的 0 保存时使用:SqlDataAdapter批量更新DataTable,怎么 ...

随机推荐

  1. java double类型保留两位小数4种方法

    http://blog.csdn.net/huaishuming/article/details/17752365 ****************************************** ...

  2. Centos7.4使用SoftEther搭建V.PN

    参考: https://blog.csdn.net/qq_39591494/article/details/78625394?locationNum=9&fps=1 https://www.b ...

  3. LeetCode: Valid Palindrome 解题报告

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  4. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  5. spring中xml配置方式和注解annoation方式(包括@autowired和@resource)的区别

    xml文件中配置itemSqlParameterSourceProvider是可以的: <bean id="billDbWriter" class="com.aa. ...

  6. TaskWarrior windows版制作

    TaskWarrior TaskWarrior是一款优秀的命令行todo神器,具体用法可以看之前我写的介绍. 但是需要cygiwn环境,而我一直在用cmder的完整包,不想再安装一个cygwin了,于 ...

  7. Postgres客户端编码问题

    数据库编程的编码问题数据库编程设计的编码问题包括三个方面:    数据库服务器编码:    数据库客户端编码:    本地环境编码.(1)数据库服务器字符编码:数据库服务器支持某种编码,是指数据库服务 ...

  8. go包管理之glide

    go语言的包是没有中央库来统一管理的,通过使用go get命令从远程代码库(github.com,goolge code 等)拉取,直接跳过中央版本库的约束,让代码的拉取直接基于源代码版本控制库,开发 ...

  9. setfacl命令

    setfacl命令是用来在命令行里设置ACL(访问控制列表).在命令行里,一系列的命令跟随以一系列的文件名. 选项 -b,--remove-all:删除所有扩展的acl规则,基本的acl规则(所有者, ...

  10. 【C】——pthread_mutex_lock

    函数名 pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock - lock and unlock a mutex SYNOPS ...