android.database.sqlite.SQLiteException:near“FROM”:语法错误(代码1):

时间:2017-04-04 05:33:26

标签: android sqlite android-sqlite

我想在android studio中使用Sqlite,我想使用以下代码使用id号搜索“Destination”对象。

private final static  String DATABASE_NAME = "Destination.db";

private final static  int  DATABASE_VERSION = 1;

//table name
private final static  String TABLE_NAME = "Destination";

public final static  String DESTINATION_ID = "destination_id";

public final static  String DESTINATION_NAME = "destination_name";

public final static  String DESTINATION_PRIORITY = "destination_priority";

public final static  String DESTINATION_LONGITUDE = "destination_longitude";

public final static  String DESTINATION_LATITUDE = "destination_latitude";

名字在上面,这是我的代码:

 public Destination getDestination(int id){
    SQLiteDatabase db=this.getReadableDatabase();

    String selectQuery="SELECT "+
            DESTINATION_ID + "," +
            DESTINATION_NAME + "," +
            DESTINATION_PRIORITY + "," +
            DESTINATION_LONGITUDE + "," +
            DESTINATION_LATITUDE + "," +
            " FROM " + TABLE_NAME
            + " WHERE " +
            DESTINATION_ID + "=?";

    Destination destination = new Destination();

    Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(id)});

    if(cursor.moveToFirst()){
        do{
            destination.setId(cursor.getInt(cursor.getColumnIndex(DESTINATION_ID)));
            destination.setName(cursor.getString(cursor.getColumnIndex(DESTINATION_NAME)));
            destination.setPriority(cursor.getInt(cursor.getColumnIndex(DESTINATION_PRIORITY)));
            destination.setLongitude(cursor.getDouble(cursor.getColumnIndex(DESTINATION_LONGITUDE)));
            destination.setLongitude(cursor.getDouble(cursor.getColumnIndex(DESTINATION_LATITUDE)));
        } while(cursor.moveToNext());
    }
    cursor.close();
    db.close();

    return destination;

}

logCAT中的信息是:  see picture here

选择查询似乎有问题:

Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(id)});

为什么?我是sqlite的初学者,请帮帮我!谢谢!

4 个答案:

答案 0 :(得分:3)

你应该从selectQuery中删除last。

String selectQuery="SELECT "+
        DESTINATION_ID + "," +
        DESTINATION_NAME + "," +
        DESTINATION_PRIORITY + "," +
        DESTINATION_LONGITUDE + "," +
        DESTINATION_LATITUDE  +
        " FROM " + TABLE_NAME
        + " WHERE " +
        DESTINATION_ID + "=?";

答案 1 :(得分:1)

试试这个删除“,”

String selectQuery="SELECT "+
            DESTINATION_ID + "," +
            DESTINATION_NAME + "," +
            DESTINATION_PRIORITY + "," +
            DESTINATION_LONGITUDE + "," +
            DESTINATION_LATITUDE  +
            " FROM " + TABLE_NAME
            + " WHERE " +
            DESTINATION_ID + "=?";

答案 2 :(得分:1)

您的字段列表中有一个逗号。

String selectQuery="SELECT "+
        DESTINATION_ID + "," +
        DESTINATION_NAME + "," +
        DESTINATION_PRIORITY + "," +
        DESTINATION_LONGITUDE + "," +
        DESTINATION_LATITUDE + "," +   // This will appear as
        " FROM " + TABLE_NAME          // "destination_latitude, FROM"
        + " WHERE " +
        DESTINATION_ID + "=?";

在DESTINATION_LATITUDE之后删除逗号,你应该好好去。

答案 3 :(得分:1)

从代码中删除额外的,并修改它

String selectQuery="SELECT "+
        DESTINATION_ID + "," +
        DESTINATION_NAME + "," +
        DESTINATION_PRIORITY + "," +
        DESTINATION_LONGITUDE + "," +
        DESTINATION_LATITUDE +
        " FROM " + TABLE_NAME
        + " WHERE " +
        DESTINATION_ID + "=?";