在sqlite android中选择查询

时间:2014-03-04 10:46:07

标签: android sqlite

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=?

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

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

3 个答案:

答案 0 :(得分:20)

存在SQL语法问题,您需要使用Cursor来检索查询结果,例如使用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();

答案 1 :(得分:3)

使用以下内容更正您的查询:在WHERE原因

添加空格
String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=? ";

更新: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();

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

答案 2 :(得分:3)

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();