how to retrieve an element in the SQL database on Android Studio?

Adrien Moinnet :

I created a database in SQL on Android Studio but I can not recover and update data in this database.

I checked a lot of site and guide but none of them give me a correct answer.

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create table user(pseudo text primary key, password text, email text, points integer, pointsshop integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists user");
    }
    //inserting in database
    public boolean insert(String pseudo, String password, String email, int points, int pointsshop){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("pseudo",pseudo);
        contentValues.put("password",password);
        contentValues.put("email",email);
        contentValues.put("points",points);
        contentValues.put("pointsshop",pointsshop);
        long ins = db.insert("user",null,contentValues);
        if(ins==-1)return false;
        else return true;
    }

    public void update_points(int points, String pseudo){
            this.getWritableDatabase().execSQL("update user set 
    points='" + points + "' where pseudo='" + pseudo + "'");
        }
        public int get_points(String pseudo){
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery("Select points from user where 
            pseudo=?",new String[]{pseudo});
            return cursor.getInt(0);
        }
    }
}

I would like to get the data "points" from my database but when i run my application, this one crashed

MikeT :

Although you've extracted a Cursor, the cursor is at a position called beforeFirstRow you need to move to a row in the cursor, if any, before you can retrieve data.

You should always close a cursor when done with it. As you aren't returning the cursor, it should be closed before returning the value extracted from the cursor.

Try the following :-

public int get_points(String pseudo) {
    int rv = -1;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("Select points from user where 
    pseudo=?",new String[]{pseudo});
    if (cursor.moveToFirst()) {
        rv = cursor.getInt(cursor.getColumnIndex("points"));
    }
    cursor.close();
    return rv;   
}
  • Note if no data is extracted then the above will return -1. This value may not be a useful indicator that no data has been extracted, so you may need to use a different value.
  • The Cursor move methods (such as moveToFirst as used above) return true if the move could be made, else false. Hence if (cursor.moveToFirst())
  • getColumnIndex has been used as it's less prone to inadvertent errors being introduced due to the mis-calculation of hard coded offsets.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160412&siteId=1