Brian Sweeney
Brian Sweeney

Reputation: 6793

Adding parameters in SQLite with C#

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

Upvotes: 30

Views: 101929

Answers (3)

ozanmut
ozanmut

Reputation: 3234

var connectionstring = "Data Source=" + Environment.CurrentDirectory + "\\game.db";
using (var connection = new SqliteConnection(connectionString))
{
    var insertCmd = connection.CreateCommand();
    insertCmd.CommandText = "INSERT INTO Results(result, win) VALUES (@prizes, @win)";
    insertCmd.Parameters.AddWithValue("prizes", prizes);
    insertCmd.Parameters.AddWithValue("win", winAmount);
    insertCmd.ExecuteNonQuery();
}

Upvotes: 0

Björn
Björn

Reputation: 29381

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));
...

Upvotes: 93

Jeff Mattfield
Jeff Mattfield

Reputation:

Try VALUES instead of VALUE.

Upvotes: 31

Related Questions