在sqlite android中选择查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22169752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:37:02  来源:igfitidea点击:

select query in sqlite android

androidsqlite

提问by Mukund

String temp_address="nothing";
    try
    {
        String selectQuery = "SELECT lastchapter FROM Bookdetails INTO"+temp_address+"WHERE bookpath=?";
        db.execSQL(selectQuery, new String[] { fileName });
        System.out.println(temp_address+" result of select Query");
    }

    catch(Exception e)
    {
        System.out.println(e+" is the error here");

    }
    finally
    {
        db.close();
    }

Logcat

逻辑猫

android.database.sqlite.SQLiteException: near "bookpath": syntax error: , while compiling: SELECT lastchapter FROM Bookdetails INTOnothingWHERE bookpath=?

i just want to take the result of the above query so that the string stored in lastchapter is available in temp_address please help

我只想获取上述查询的结果,以便存储在 lastchapter 中的字符串在 temp_address 中可用,请帮助

i am new to android sqlite database please help

我是 android sqlite 数据库的新手,请帮忙

回答by laalto

There are SQL syntax problems and you'll need to use a Cursorto retrieve query results, for example with rawQuery():

存在 SQL 语法问题,您需要使用 aCursor来检索查询结果,例如rawQuery()

String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";
Cursor c = db.rawQuery(selectQuery, new String[] { fileName });
if (c.moveToFirst()) {
    temp_address = c.getString(c.getColumnIndex("lastchapter"));
}
c.close();

回答by M D

Correct your query with below: add space at WHERECause

使用以下更正您的查询:在WHERECause 中添加空格

String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=? ";

Update:go with rawQuery()becoz it's return Cursorwith results

更新:rawQuery()becoz一起返回Cursor结果

 String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=? ";
 Cursor c = db.rawQuery(selectQuery, new String[] { fileName });
 if (c.moveToFirst()) {
 temp_address = c.getString(0);
 }
  c.close();

And for more information go to this: http://www.higherpass.com/android/tutorials/accessing-data-with-android-cursors/

有关更多信息,请访问:http: //www.higherpass.com/android/tutorials/accessing-data-with-android-cursors/

回答by nikis

The logcat said it all, you've forgot spaces. To get data into the string:

logcat 已经说明了一切,您忘记了空格。将数据放入字符串中:

String temp_address="nothing";
String[] args = new String[] { fileName };
Cursor cursor = sqLiteDatabase.rawQuery("SELECT lastchapter FROM Bookdetails WHERE bookpath=?", args);
if (cursor.moveToFirst()){
    temp_address = cursor.getString(cursor.getColumnIndex("lastchapter"));
}
cursor.close();