-- 演示将多条记录数据组合成一条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. VMware文章总结

    Vmware Vsphere6.5 + Vcenter6.5安装简介:http://www.ctoclubs.com/?p=296 安装VCSA6.5(vCenter Server Appliance ...

  2. Python爬网——获取安卓手机统计数据

    [本文出自天外归云的博客园] 1. 在安卓网上对热门机型进行爬网,取前五十: # -*- coding: utf-8 -*- import requests,re from bs4 import Be ...

  3. python dataframe astype 字段类型转换

    使用dtype查看dataframe字段类型 print df.dtypes 使用astype实现dataframe字段类型转换 # -*- coding: UTF-8 -*- import pand ...

  4. 查看chekpoit文件

    使用tf.train.Saver()保存到checkpoint文件,我们可以用tensorflow查看. # import the inspect_checkpoint library from te ...

  5. java中ThreadExecutor使用注意

    如果使用了submit(Runnable task) 就会出现这种情况,任何的错误信息都出现不了! 这是因为使用submit(Runnable task) 的时候,错误的堆栈信息跑出来的时候会被内部捕 ...

  6. WebApi增删改查Demo

    1.新建webapi项目 2.配置WebApiConfig public const string DEFAULT_ROUTE_NAME = "MyDefaultRoute"; p ...

  7. WCF数据契约

    当使用DataMember时,和访问符无关,及时使用了private,成员都是可见的.相反如果使用static,为不可见. 上述的两个数据成员是等效的,如果是等效的话 数据成员的顺序也必须是相同的. ...

  8. [转]bigdecimal 保留小数位

    原文地址:https://www.cnblogs.com/liqforstudy/p/5652517.html public class test1_format { public static vo ...

  9. C语言 · 计算时间

    算法提高 计算时间   时间限制:1.0s   内存限制:512.0MB      问题描述 给定一个t,将t秒转化为HH:MM:SS的形式,表示HH小时MM分钟SS秒.HH,MM,SS均是两位数,如 ...

  10. win10老提示系统错误,要注销

    win10老提示系统错误,要注销? 开启user manager 服务(对我没用) 用administrator账户(成功,不提示了) 或者创建一个新账户(未测)