如何限制sqllite查询?

时间:2018-06-14 11:42:30

标签: android sql sqlite

 public String readQuestion(int i)//Used to read the data from the Des.db file where id is given and we choose id randomly
{
    String Ans = "";//string that contains the required field  note that Ans is just a local string not related to Answer or Option...
    Cursor c = sqlite.rawQuery("SELECT " + Question + " FROM " + Table_name + " LIMIT " + 5 + " WHERE " + uid + " = " + i + "", null);//cursor to that query
    if (c.moveToFirst())
        Ans = c.getString(0);
    else
        Ans = "";
    return Ans;
}

我想在此查询中添加限制。像LIMIT 10。

3 个答案:

答案 0 :(得分:1)

你关闭了。 LIMIT关键字必须位于查询的结束位置。你现在的位置:

"SELECT " + Question + 
" FROM " + Table_name + 
" LIMIT " + 5 + 
" WHERE " + uid + " = " + i + ""

这应该是

"SELECT " + Question + 
" FROM " + Table_name + + 
" WHERE " + uid + " = " + i +
" LIMIT " + 5 + ""

答案 1 :(得分:0)

请考虑小改动。

Cursor c = sqlite.rawQuery("SELECT " + Question + " FROM " + Table_name  
           + " WHERE  " + uid + " = " + i  + " LIMIT " + "5", null);

答案 2 :(得分:0)

Cursor c = sqlite.rawQuery("SELECT " + Question + " FROM " + Table_name + " WHERE " + uid + " = " + i + " LIMIT 5 ", null);

限制位于查询的末尾。您应该查看链接以获取详细信息。 sqlite limit statement