I'm attempting to insert data from an object into my sqlite database table. I keep on receiving the same error when attempting to do so.
When inserting data into a different table (words) of the same db, using the same technique, I am able to successfully insert the data without errors. This leads me to believe that my SQLiteConnection value 'cnn' is not the issue. I have ensured that the names of the object properties are the same, as well as the fields within the table. There is no primary key within this specific table, but I'm not sure if that's a problem or not.
The code that doesnt work:
using (IDbConnection cnn = new SQLiteConnection(connection))
{
foreach (bridgeRecord br in bridgeWords)
{
try
{
cnn.Execute("insert into bridge (engWord, spaWord, frequency, wordClass) values (@engWord, @spaWord, @frequency, @wordClass)", br);
}
catch (SQLiteException ex)
{
Console.WriteLine(ex);
}
}
}
The code that does work:
using (IDbConnection cnn = new SQLiteConnection(connection))
{
foreach (Word w in words)
{
try
{
cnn.Execute("insert into words (word, wordSimplified, confidence, difficulty, wordClass, wordCategory, dateTestedLast, popularity, language) " +
"values (@word, @wordSimplified, @confidence, @difficulty, @wordClass, @wordCategory, @dateTestedLast, @popularity, @language)", w);
}
catch (SQLiteException ex)
{
wordsBouncedBack.Add(w.word);
continue;
}
}
}
The 'bridgeRecord' class model looks like this:
class bridgeRecord
{
public string engWord;
public string spaWord;
public int frequency;
public string wordClass;
}
This is the error i receive:
code = Unknown (-1), message = System.Data.SQLite.SQLiteException (0x80004005): unknown error
Insufficient parameters supplied to the command
at System.Data.SQLite.SQLiteStatement.BindParameter(Int32 index, SQLiteParameter param)
I expected the 'bridgeRecord' object to provide the parameters to be inserted but this is not the case. Although the 'Word' object seems to provide the parameters just fine which confuses me greatly.
Any help would be very much appreciated. This is my first stack overflow question so I'm sorry if the answer is extremely obvious :)
Taking the advice of Pascal in the comments, I used the command.parameters.add method to fix my issue. I prepared the statment before-hand and then added the parameters into their correct place. The final code now looks like this:
SQLiteCommand command = new SQLiteCommand("insert into bridge (id, engWord, spaWord, frequency, wordClass) values (@id, @engWord, @spaWord, @frequency, @wordClass)",cnn);
command.Parameters.AddWithValue("@id", br.engWord + br.spaWord + br.frequency + br.wordClass);
command.Parameters.AddWithValue("@engWord", br.engWord);
command.Parameters.AddWithValue("@spaWord", br.spaWord);
command.Parameters.AddWithValue("@frequency", br.frequency);
command.Parameters.AddWithValue("@wordClass", br.wordClass);
command.ExecuteNonQuery();
It would of been better to find a fix which enabled the code to work like the other INSERT statement, but this work around will suffice.
User contributions licensed under CC BY-SA 3.0