[Lua, MySQL] combines multiple record data into a single SQL INSERT statement (for MySQL)

Source: Internet
Author: User
Tags lua

--demonstrates combining multiple record data into one SQL INSERT statement (for MySQL) function getTpl0 (tname)--Get table fields Local T = {Tpl_pack = {"Packid", "ItemId", "' Group '", "num", "rate", "Ratetype"},}for K, V in pairs (t) doif tname = = k Thenreturn T[k]endendend--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, 90 04, ', 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} -"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},{91 113, ",", 2},}function createinsertsql (Tname, TPL) Local tpl0 = GetTpl0 (tname)--Get table fields local ret = {}--Insert record SQL Table.insert (ret, String.Format ("insert INTO '%s ' (", tname)) for K, V in Pairs (tpl0) doif k > 1 thentable.insert (ret, ", ") Endtable.insert (ret, v) endtable.insert (ret,") values ") for K, V in Pairs (TPL) Doif k > 1 thentable.insert (ret,", ") E Ndtable.insert (ret, "(") for K2, V2 in pairs (v) doif K2 > 1 thentable.insert (ret, ",") endif type (v2) = = "string" Thentab Le.insert (ret, String.Format ("'%s '", V2)) Elsetable.insert (ret, v2) Endendtable.insert (ret, ")") Endtable.insert (ret, ";") Local result = Table.concat(ret)--final SQL statement print (result) print () endcreateinsertsql ("Tpl_pack", tpl9122) createinsertsql ("Tpl_pack", tpl9123) 

  The results of the final execution are as follows:

[[email] 6] $lua t1.luainsert 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] combines multiple record data into a single SQL INSERT statement (for MySQL)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.