www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Request our suggestio: better way to insert data from

reply Vino <akashvino79 gmail.com> writes:
Hi All,

   Request your suggestion, we have a program which call's an api, 
the output of the api is parsed using json parser and the result 
is stored in an array(Array!string[string] data), then these 
stored result are inserted into MySQL table, for inserting the 
data into the table we use the below code, this code is a small 
code which just contains 2 data items (Type,Hostname) and we have 
similar api's which  contains 15-20 data items, hence request 
your suggestion on is there any better way than the below code, 
using the below logic the foreach line is will run into multiple 
lines eg:

foreach(i, ref a, ref b,.......ref 20, 
lockstep(data["D1"][],data["D2"][],.....data["D20"])

Code:

  Statement stmt = con.db.prepare("INSERT INTO test(Type,Hostname) 
VALUES(:Type,:Hostname");
  foreach(i,ref t,ref h; lockstep(data["Type"][], 
data["Hostname"][]) {
     stmt.setParameter("Type", data["Type"][i]);
     stmt.setParameter("Hostname", data["Hostname"][i]);
     result =  stmt.execute();
}


From,
Vino.B
Nov 16 2020
parent reply Max Haughton <maxhaton gmail.com> writes:
On Monday, 16 November 2020 at 17:44:08 UTC, Vino wrote:
 Hi All,

   Request your suggestion, we have a program which call's an 
 api, the output of the api is parsed using json parser and the 
 result is stored in an array(Array!string[string] data), then 
 these stored result are inserted into MySQL table, for 
 inserting the data into the table we use the below code, this 
 code is a small code which just contains 2 data items 
 (Type,Hostname) and we have similar api's which  contains 15-20 
 data items, hence request your suggestion on is there any 
 better way than the below code, using the below logic the 
 foreach line is will run into multiple lines eg:

 [...]
What are you looking to improve? Do you want to make the code prettier or faster? It doesn't look too bad to my eye although my personal style would be to unpack t and h inside the foreach loop.
Nov 16 2020
parent Vino <akashvino79 gmail.com> writes:
On Monday, 16 November 2020 at 18:30:01 UTC, Max Haughton wrote:
 On Monday, 16 November 2020 at 17:44:08 UTC, Vino wrote:
 Hi All,

   Request your suggestion, we have a program which call's an 
 api, the output of the api is parsed using json parser and the 
 result is stored in an array(Array!string[string] data), then 
 these stored result are inserted into MySQL table, for 
 inserting the data into the table we use the below code, this 
 code is a small code which just contains 2 data items 
 (Type,Hostname) and we have similar api's which  contains 
 15-20 data items, hence request your suggestion on is there 
 any better way than the below code, using the below logic the 
 foreach line is will run into multiple lines eg:

 [...]
What are you looking to improve? Do you want to make the code prettier or faster? It doesn't look too bad to my eye although my personal style would be to unpack t and h inside the foreach loop.
Hi Max, I am looking to improve the performance and any quicks that can replace the above code, similar to the below topic https://forum.dlang.org/post/kkyzysjpcrdovafmrhlq forum.dlang.org
Nov 16 2020