I’ve got an SQLite table that I created for a new project.
The table’s CREATE and INSERT commands are defined as follows:
public const string SQL_CREATE = "CREATE TABLE Tanks " +
"(LocID INTEGER, InUse INTEGER, Text1 TEXT, Description1 TEXT);";
public const string SQL_INSERT = "INSERT INTO Tanks " +
"(LocID, InUse, Text1, Description1) " +
"VALUES " +
"@LocID, @InUse, @Text, @Description); ";
Whenever I attempt to save my first values to the table, I get the unhelpful message, “SQLite error near “@LocID” syntax error.”
Here is my insert routine:
public static Tank New(Tank tank) {
tank.Result = 0;
using (SQLiteConnection con = GetConnection) {
try {
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(Tank.SQL_INSERT, con)) {
cmd.Parameters.AddWithValue("@LocID", tank.LocationID); // = 1
cmd.Parameters.AddWithValue("@InUse", tank.InUse); // = 1 (for true)
cmd.Parameters.AddWithValue("@Text", tank.Text); // says "New Tank"
cmd.Parameters.AddWithValue("@Description", DBNull.Value); // I've tried "New Tank" and DBNull.Value
try {
tank.Result = cmd.ExecuteNonQuery();
} catch (SQLiteException err) {
tank.Result = -1;
LogError("New(Tank)", err);
throw err;
}
}
} finally {
con.Close();
}
}
return tank;
}
I did not create the database with the index and I am not inserting an index value because the documentation says: “every row of every table has an 64-bit signed integer ROWID. The ROWID for each row is unique among all rows in the same table.” A little further down in the same page, it says, “If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, then an appropriate ROWID is created automatically.” Source: sqlite.org/autoinc
The error is thrown on ExecuteNonQuery().
What have I done wrong?
Using SQLite 3 in Win7 Visual Studio 2008 (C#), targeting Windows Mobile 5 (Pocket PC).
You’re missing the opening parenthesis before @LocID in the SQL_INSERT string.