I want to insert an array into a table.
Each array element is a row in the table. How can I achieve my goal?
I don't want to use
VALUES x1,x2,x3,x4 ......
I want to insert an array into a table.
Each array element is a row in the table. How can I achieve my goal?
I don't want to use
VALUES x1,x2,x3,x4 ......
You can use a for loop to operate on each element of the array. I'm assuming it's a one (meaningful) columned table.
//len is length of array
for (i=0; 0<len; i++){
//create SQL statement
sql="INSERT INTO table (row1) VALUES " . array[i];
//[execute sql here]
}
Alternatively, you could do it like so, if it will work with your version of SQL:
//all inserts are done in the one SQL statement
int len = //length of array
//the start of the statement. The first element is done here because
//one less comma is required then the number of elements
string sql = "INSERT INTO table (row1) VALUES (" . array[0];
for (int i=1; 0<len; i++){
//create SQL statement
sql .= "), VALUES (" . array[i];
}
sql .= ");";
//[execute sql here]
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.