Adding parameters in SQLite with C#

Brian Sweeney picture Brian Sweeney · Apr 30, 2009 · Viewed 79.5k times · Source

Im just learning SQLite and I can't get my parameters to compile into the command properly. When I execute the following code:

this.command.CommandText = "INSERT INTO [StringData] VALUE (?,?)";

this.data = new SQLiteParameter();
this.byteIndex = new SQLiteParameter();

this.command.Parameters.Add(this.data);
this.command.Parameters.Add(this.byteIndex);

this.data.Value = data.Data;
this.byteIndex.Value = data.ByteIndex;

this.command.ExecuteNonQuery();

I get a SQLite Exception. Upon inspecting the CommandText I discover that whatever I'm doing is not correctly adding the parameters: INSERT INTO [StringData] VALUE (?,?)

Any ideas what I'm missing?

Thanks

Answer

Björn picture Björn · Apr 30, 2009

Try a different approach, naming your fields in the query and naming the parameters in the query:

this.command.CommandText = "INSERT INTO StringData (field1, field2) VALUES(@param1, @param2)";
this.command.CommandType = CommandType.Text;
this.command.Parameters.Add(new SQLiteParameter("@param1", data.Data));
this.command.Parameters.Add(new SQLiteParameter("@param2", data.ByteIndex));
...