使用C#在SQLite中添加参数

时间:2023-01-17 13:48:39

Im just learning SQLite and I can't get my parameters to compile into the command properly. When I execute the following code:

我刚刚学习SQLite,我无法正确编译命令。当我执行以下代码时:

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 (?,?)

我得到一个SQLite异常。在检查CommandText时,我发现无论我做什么都没有正确添加参数:INSERT INTO [StringData] VALUE(?,?)

Any ideas what I'm missing?

我缺少什么想法?

Thanks

谢谢

2 个解决方案

#1


19  

Try VALUES instead of VALUE.

尝试VALUES而不是VALUE。

#2


65  

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

#1


19  

Try VALUES instead of VALUE.

尝试VALUES而不是VALUE。

#2


65  

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