0

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 ......
2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by humbug
0

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]

http://en.wikipedia.org/wiki/Insert_(SQL)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.