I am trying to execute an SQLiteCommand containing 3 inserts to 3 separate tables. In all 3 tables the primary key is called offset. In the 3rd statement, I want to insert two rows.
There are a total of 11 parameters to the command
Here is the code I used to initialize the command earlier (I want to reuse the command, but this particular problem occurs even on the first use of this statement):
_insertBlockInNewPage = new SQLiteCommand("insert into page(offset) values (?); insert into block(offset, length, originaloffset, page) values (?, ?, ?, ?); insert into chunk(offset, length, page, free) values (?, ?, ?, 0), (?, ?, ?, 1)", _db);
for (var i = 0; i < 11; i++) _insertBlockInNewPage.Parameters.Add(new SQLiteParameter(DbType.Int64));
_insertBlockInNewPage.Prepare();
And here is the method that will use this command:
public void InsertBlockInNewPage(long offset, long length, long originalOffset, long pageLength)
{
_insertBlockInNewPage.Parameters[0].Value = offset;
_insertBlockInNewPage.Parameters[1].Value = offset;
_insertBlockInNewPage.Parameters[2].Value = length;
_insertBlockInNewPage.Parameters[3].Value = originalOffset;
_insertBlockInNewPage.Parameters[4].Value = offset;
_insertBlockInNewPage.Parameters[5].Value = offset;
_insertBlockInNewPage.Parameters[6].Value = length;
_insertBlockInNewPage.Parameters[7].Value = offset;
_insertBlockInNewPage.Parameters[8].Value = offset + length;
_insertBlockInNewPage.Parameters[9].Value = pageLength - length;
_insertBlockInNewPage.Parameters[10].Value = offset;
_insertBlockInNewPage.ExecuteNonQuery();
}
What is important here that parameter 5 has the value 0 and parameter 8 has the value 131072 (i.e. not the same). When I execute this command it throws an Exception telling me that the primary key must be unique.
What’s weird is that if I only insert one row in the chunk table it works. What’s weirder is if I execute the insert of the chunk table in a separate SQLiteCommand, but still inserting 2 rows simultaneously it works too. So i’m guessing I must have done something wrong with the parameters. But I really cannot see anything wrong with this code. Can anyone help me with this?
In SQLite, parameter indexes are counted from the start of the SQL statement.
This implies that it is not possible to use unnamed parameters together with multiple SQL commands in one
SQLCommand.You could try to use numbered parameters: