Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
What is the best way to insert an array of variable length of data into a sql database?

So I have a form of variable size and users can add any amount of fields they like.

an example of form values is like this:

{variableSizedArray: [
    {name: car1Name, age: car1Age}, 
    {name: car2Name, age: car2Age}, 
    {name: car3Name, age: car3Age},
    ...
]}

How would I go about inserting each value into my SQL database?

Things I tried:

  1. Executing a SQL statement for each value:
     variableSizedArray.forEach((item) => {
         db.exec("INSERT INTO cars (name, age) VALUES (?, ?)", [item.name, item.age]); 
     })

But this will cause many calls to the database which I believe is bad practice?.

  1. Creating a custom string:
     variableSizedArray.forEach((item) => {
         insertString += `(${item.name}, ${item.age}),`;
     }
     db.exec(`INSERT INTO cards (name, age) VALUES ${insertString}`); 

this is the solution I am using right now but it's getting quite complicated since I have much more variables in my project and I need to create a different "custom string" builder for every new "operation" I make.

Are those the only 2 options and is my understanding of their drawbacks correct?

Thanks in advance.

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!