4

我总是得到这个错误

“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 * from mey”,请在“x”附近说:语法错误

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

4

3 回答 3

6

When str has a value containing an apostrophe, this query will not work because the string will be terminated too early:

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

You must use parameters:

cmd = new SQLiteCommand("select * from mey where trans like @pattern", litecon);
cmd.Parameters.AddWithValue("@pattern", "%" + str + "%");
于 2013-06-24T16:45:01.923 回答
3

问题通常与撇号有关。您可以使用它们,但必须像另一个答案中提到的那样加倍,

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("'","''")

这无论如何都会成功。

于 2014-05-19T17:21:02.690 回答
0

问题解决了!

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

多谢你们

于 2013-06-25T06:41:37.837 回答