嗨,下面的代码给出了一个语法错误。我不知道如何解决这个问题。
错误
{"SQLite error\r\nnear \"Mytext\": syntax error"}
我的代码
string dataSource = "Database.s3db";
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + dataSource;
connection.Open();
SQLiteCommand command = new SQLiteCommand(connection);
command.CommandText = ("update Example set Info ='" + textBox2.Text + ", Text ='"+textBox3.Text + "where ID ='" + textBox1.Text +"'");
command.ExecuteNonQuery();
最佳答案
其他人提出了构建 SQL 的替代方法,但您根本不应该在 SQL 中包含这些值。您应该使用参数化查询,这样可以避免 SQL injection attacks除其他事项外。
我不是很清楚您使用的是哪个驱动程序,但假设它是 Devart.com 驱动程序,SQLiteCommand.Parameters
的文档给出了一个很好的例子来说明如何做到这一点。在您的情况下,代码将变为:
string dataSource = "Database.s3db";
using (SQLiteConnection connection = new SQLiteConnection())
{
connection.ConnectionString = "Data Source=" + dataSource;
connection.Open();
using (SQLiteCommand command = new SQLiteCommand(connection))
{
command.CommandText =
"update Example set Info = :info, Text = :text where ID=:id";
command.Parameters.Add("info", DbType.String).Value = textBox2.Text;
command.Parameters.Add("text", DbType.String).Value = textBox3.Text;
command.Parameters.Add("id", DbType.String).Value = textBox1.Text;
command.ExecuteNonQuery();
}
}
关于c# - Sqlite "Update"C#语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556135/