C# 如何修复';SQLite异常,为命令提供的参数不足';使用INSERT语句时
我正试图将对象中的数据插入sqlite数据库表。在尝试这样做时,我不断收到相同的错误 当使用相同的技术将数据插入到同一数据库的不同表(字)中时,我能够成功地插入数据而不会出错。这让我相信我的SQLiteConnection值“cnn”不是问题所在。我已经确保对象属性的名称以及表中的字段是相同的。这个特定表中没有主键,但我不确定这是否有问题 不起作用的代码:C# 如何修复';SQLite异常,为命令提供的参数不足';使用INSERT语句时,c#,sql,sqlite,C#,Sql,Sqlite,我正试图将对象中的数据插入sqlite数据库表。在尝试这样做时,我不断收到相同的错误 当使用相同的技术将数据插入到同一数据库的不同表(字)中时,我能够成功地插入数据而不会出错。这让我相信我的SQLiteConnection值“cnn”不是问题所在。我已经确保对象属性的名称以及表中的字段是相同的。这个特定表中没有主键,但我不确定这是否有问题 不起作用的代码: using (IDbConnection cnn = new SQLiteConnection(connection)) { fo
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);
}
}
}
有效的代码:
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;
}
}
}
“bridgeRecord”类模型如下所示:
class bridgeRecord
{
public string engWord;
public string spaWord;
public int frequency;
public string wordClass;
}
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();
这是我收到的错误:
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)
我希望“bridgeRecord”对象提供要插入的参数,但事实并非如此。虽然“Word”对象似乎提供的参数很好,这让我非常困惑
任何帮助都将不胜感激。这是我的第一个堆栈溢出问题,如果答案非常明显,我很抱歉:)根据Pascal在评论中的建议,我使用command.parameters.add方法解决了我的问题。我事先准备好状态,然后将参数添加到正确的位置。最后的代码如下所示:
class bridgeRecord
{
public string engWord;
public string spaWord;
public int frequency;
public string wordClass;
}
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();
最好找到一个修复程序,使代码能够像其他INSERT语句一样工作,但这种解决方法就足够了。helo,您能打印一些数据吗(bridgeRecord)。你的数据上可能有空值吗?另一方面,您可以尝试使用命令.Parameters.Add(newsqliteparameter(“@Name”,“Has”));嗨,在调试时,我可以看到在执行SQL语句之前bridgeRecord没有空值。是的,我考虑过使用“command.Parameters.add”方法,但是对每个参数这样做似乎有点笨重。不过,这可能是唯一的解决方案,谢谢您的推荐。谢谢您的回复是的,这可能是第一个查看它产生错误的方法,可能是缺少转换数据类型或其他原因。很抱歉,我的计算机上没有复制的基本配置。您尝试过SqliteParameter吗?是的,我现在使用的是:command.Parameters.AddWithValue(“engword”,br.engword);它工作得很好,虽然在声明前后我都必须打开和关闭db连接,但这不是问题。非常感谢您的帮助@pascal