Home > Software engineering >  Impossible to get data from database Sqlite in Android Studio
Impossible to get data from database Sqlite in Android Studio

Time:01-06

i am trying to update a value and fetch it from a sqlite database :

    private static final String TABLE_INTERVALLE_DE_MESURE = "intervalle_de_mesure";
    private static final String COLONNE_ID = "_id";
    private static final String COLONNE_VALEUR = "valeur";

    private static final String CREATE_INTERVALLE_DE_MESURE =
            "create table "   TABLE_INTERVALLE_DE_MESURE   " ("  
                    COLONNE_ID   " integer primary key autoincrement, "
                      COLONNE_VALEUR   " integer default 0);";


    public MaBD_IntervalleDeMesure (Context context) {
        super(context, dbName, null, dbVersion) ;
    }

    public void updateIntervalle(int intervalle) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLONNE_VALEUR, intervalle);

        db.update(TABLE_INTERVALLE_DE_MESURE, values, COLONNE_ID   " = ?", new String[] {Long.toString(1)});
        db.close();
    }

    public int getIntervalle() {
        int res = 0;
        SQLiteDatabase db = this.getReadableDatabase();

        String maRequeteIntervalle = "SELECT "   COLONNE_VALEUR   " FROM "    TABLE_INTERVALLE_DE_MESURE   ";";

        Cursor cursor = db.rawQuery(maRequeteIntervalle, null);
        if (cursor.getCount() != 0) {
            cursor.moveToFirst(); 
            res = cursor.getInt(0);
        }

        cursor.close();
        db.close();
        return res;
    }
}

the return of db.update in the updateIntervalle function returns me a number other than -1.

However, cursor.getCount() returns me 0. Why ?

CodePudding user response:

the return of db.update in the updateIntervalle function returns me a number other than -1.

There appears to be nothing wrong with the code. However, the update convenience method will never return -1. It returns the number of rows that have been updated or 0 if none.

However, cursor.getCount() returns me 0. Why ?

At a guess, because there is no data in the table.

  • Perhaps you are assuming that the update method will add a row. It will not, it will only update existing rows. If so then you will need to insert a row.

Demonstration

Using the following adaptation of your code :-

class MaBD_IntervalleDeMesure extends SQLiteOpenHelper {

    public static final String TABLE_INTERVALLE_DE_MESURE = "intervalle_de_mesure";
    public static final String COLONNE_ID = "_id";
    public static final String COLONNE_VALEUR = "valeur";
    private static final String DBNAME = "mydb";
    private static final int DBVERSION = 1;

    private static final String CREATE_INTERVALLE_DE_MESURE =
            "create table "   TABLE_INTERVALLE_DE_MESURE   " ("  
                    COLONNE_ID   " integer primary key autoincrement, "
                      COLONNE_VALEUR   " integer default 0);";


    public MaBD_IntervalleDeMesure (Context context) {
        super(context, DBNAME, null, DBVERSION) ;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_INTERVALLE_DE_MESURE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void updateIntervalle(int intervalle) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLONNE_VALEUR, intervalle);

        int rv = db.update(TABLE_INTERVALLE_DE_MESURE, values, COLONNE_ID   " = ?", new String[] {Long.toString(1)});
        Log.d("DBINFO","Result of updateIntervalle is "   rv   " (0 = nothing update > 0 = number of rows updated");
        //db.close(); closing database is inefficient
    }

    public int getIntervalle() {
        int res = 0;
        SQLiteDatabase db = this.getReadableDatabase();

        String maRequeteIntervalle = "SELECT "   COLONNE_VALEUR   " FROM "    TABLE_INTERVALLE_DE_MESURE   ";";

        Cursor cursor = db.rawQuery(maRequeteIntervalle, null);
        if (cursor.getCount() != 0) {
            cursor.moveToFirst();
            res = cursor.getInt(0);
        }
        cursor.close();
        //db.close(); again closing is inefficient
        return res;
    }
}

along with an activity MainActivity as :-

public class MainActivity extends AppCompatActivity {

    MaBD_IntervalleDeMesure dbhelper;
    SQLiteDatabase db;
    public static final String TAG = "DBINFO";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // TEST 1 - Get from empty table
        dbhelper = new MaBD_IntervalleDeMesure(this);
        Log.d(TAG,"Test 1 - empty database results in "   dbhelper.getIntervalle());
        // TEST 2 - Update noithing as empty table
        Log.d(TAG,"Test 2 - update of empty database (check log for result logged by update)");
        dbhelper.updateIntervalle(1);
        // TEST 3 - Insert new row (id will very likely be 1)
        ContentValues cv = new ContentValues();
        cv.put(MaBD_IntervalleDeMesure.COLONNE_VALEUR,999);
        db = dbhelper.getWritableDatabase();
        db.insert(MaBD_IntervalleDeMesure.TABLE_INTERVALLE_DE_MESURE,null,cv);
        Log.d(TAG,"Test 3 - update of empty database (check log for result logged by update)");
        dbhelper.updateIntervalle(888);
        Log.d(TAG,"Test 4 - database with a single row results in "   dbhelper.getIntervalle());
    }
}

Then the log includes:-

2022-01-06 08:12:03.314 D/DBINFO: Test 1 - empty database results in 0
2022-01-06 08:12:03.314 D/DBINFO: Test 2 - update of empty database (check log for result logged by update)
2022-01-06 08:12:03.315 D/DBINFO: Result of updateIntervalle is 0 (0 = nothing update > 0 = number of rows updated
2022-01-06 08:12:03.315 D/DBINFO: Test 3 - update of empty database (check log for result logged by update)
2022-01-06 08:12:03.316 D/DBINFO: Result of updateIntervalle is 1 (0 = nothing update > 0 = number of rows updated
2022-01-06 08:12:03.317 D/DBINFO: Test 4 - database with a single row results in 888

So:-

Test 1 runs your getIntervalle method which returns 0 as there is no data in the table at this stage. Test 2 reports that the updateIntervalle method does nothing, as there are no rows in the table to update. TEST 3 reports, as now there is a suitable row (who's id is 1), that 1 row was updated. TEST 4 reports that the value 888 (updated from 999) has been extracted.

Additionally is App Inspection is used then the database can be viewed it shows:-

enter image description here

  •  Tags:  
  • Related