db android studio中的原始查询

inb24sb2  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(211)

我试图在android中为数据库中的位置选择一列,以访问它返回的值。我得到以下错误 Caused by: android.database.sqlite.SQLiteException: near ".": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT LAST_POSITION FROM POSITION_TABLE WHERE 23333.mkv =? in db.rawQuery() . 我想根据项目id选择col_last_position列。我应该如何更改查询顺序以从readposition()获得正确的值?

public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "PositionManager.db";
    public static final String TABLE_NAME = "POSITION_TABLE";
    private static final String ITEM_ID= "item_id";
    private static final String COL_LAST_POSITION = "LAST_POSITION";

    public DBHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String create_table = "CREATE TABLE " + TABLE_NAME + "( "
                + "ID INTEGER PRIMARY KEY ,"
                + COL_LAST_POSITION + " TEXT NOT NULL, " +
                ITEM_ID+ " TEXT NOT NULL)";
        db.execSQL(create_table);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop Table if exists " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertPosition(String item_id,int last_position){
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(ITEM_ID,item_id);
        contentValues.put(COL_LAST_POSITION,last_position);
        long result = database.insert(TABLE_NAME,null,contentValues);
        if (result == -1){
            return false;
        } else {
            return true;
        }

    }

    public boolean updatePosition(String item_id,int last_position){
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(ITEM_ID,item_id);
        contentValues.put(COL_LAST_POSITION,last_position);
        long result = database.update(TABLE_NAME , contentValues , ITEM_ID+ " =? " ,
                new String[]{item_id});
        if (result == -1){
            return false;
        } else {
            return true;
        }

    }
    public int readPosition(String item_id,int last_position)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT " + COL_LAST_POSITION + " FROM "
                        +TABLE_NAME+" WHERE " + item_id+" =? " , new String[] {String.valueOf(item_id)});

        if(cursor.moveToFirst()){
            return cursor.getInt(cursor.getColumnIndex(COL_LAST_POSITION));
        } else {
            return 0;
        }
    }
}
ubof19bj

ubof19bj1#

你犯了一个小错误。代替 item_id 具有 ITEM_ID 在您的查询中。

Cursor cursor = db.rawQuery("SELECT " + COL_LAST_POSITION + " FROM "
                            +TABLE_NAME+" WHERE " + ITEM_ID +" =? " , new String[] {String.valueOf(item_id)});

相关问题