Good day guys, I have a question:
Is it the normal behaviour for the parameters in parameterized queries to only support 1 item?
Here's my scenario: I need to write an INSERT statement to add an array of doubles, but I do not know the number of elements beforehand. So I can't just declare fixed parameters like "@data1, @data2, @data3". It has to be flexible.
The code I tried (which failed to run) is something like:
public void StoreData(double[ data)
{
// Open DB connection
using (IDbTransaction transaction = m_dbConn.BeginTransaction())
{
//Parameterized queries to speed up inserting
using (IDbCommand command = m_dbConn.CreateCommand())
{
/* Use a StringBuilder to build the list of column names so it will come out like "column1, column2, column3", ready to be inserted into INSERT statement */
/* Use a StringBuilder to build the list of column values so it will come out like "23.4, 15, 8.95", ready to be inserted into INSERT statement */
command.Transaction = transaction;
command.CommandText = "INSERT INTO '" + m_tableName + "' (@columnNames) VALUES (@columnValues)";
IDataParameter columnNames = new SQLiteParameter("@columnNames");
IDataParameter columnValues = new SQLiteParameter("@columnValues");
command.Parameters.Add(columnNames);
command.Parameters.Add(columnValues);
columnNames.Value = string_columnNames;
columnValues.Value = string_columnData;
command.ExecuteNonQuery();
}
transaction.Commit();
}
}
That code fails with the syntax error exception on @columnNames parameter. If i'm not mistaken, it's the presence of the comma that's tripping it up? It only accepts 1 value?
So how will you guys tackle this problem? I do not want to use an in-line command like:
"INSERT INTO " + m_tableName + " (" + stringColumnNames + ") VALUES (" + stringColumnValues + ")"
Obviously that will be slow as the data can be coming in up to 250000 points per second!
Would appreciate the community's thoughts on going about this the right way. Thanks!