SQLite错误:接近“s”:语法错误

时间:2013-06-24 16:10:05

标签: c# sqlite

我总是得到这个错误

  

near" s":语法错误

我的实施:

litecon.ConnectionString = "Data Source=" + AppAddress + "\\database\\mynew22.db;Version=3;UTF16Encoding=True";

从此方法中收到错误 - > liteda.Fill(DT);

        if (lang == "FaToMe")
            liteda.SelectCommand = new SQLiteCommand("select * from mey where trans like '%" + str + "%'", litecon);
        else
            liteda.SelectCommand = new SQLiteCommand("select * from mey where pe like '%" + str + "%'", litecon);

        DataTable dt = new DataTable();
        liteda.Fill(dt); //liteda is SQLiteDataAdapter

此选择命令之间没有区别......

"select * from mey where pe like '%" + str + "%'"

"select eng "

总是说" s":语法错误

但如果使用" xselect *来自mey",请说近" x":语法错误

我正在使用这个库 http://adodotnetsqlite.sourceforge.net/

3 个答案:

答案 0 :(得分:2)

str的值包含撇号时,此查询将无效,因为该字符串将过早终止:

select * from mey where trans like '%King Solomon's Mines%'

必须使用参数:

cmd = new SQLiteCommand("select * from mey where trans like @pattern", litecon);
cmd.Parameters.AddWithValue("@pattern", "%" + str + "%");

答案 1 :(得分:0)

问题解决了!

使用这些库,问题解决了...... http://www.devart.com/dotconnect/sqlite/download.html

谢谢你们

答案 2 :(得分:0)

问题通常在于撇号。您可以使用它们,但必须加倍,如另一个答案所述,

select * from mey where trans like '%King Solomon's Mines%' 应该改为 select * from mey where trans like '%King Solomon''s Mines%'

快速解决方案是"select * from mey where trans like "+"'%King Solomon's Mines%'".Replace("'","''")

无论如何,这都会成功。