samedi 7 mars 2015

Empty SQLite Database after row inserting

I want to store data into an SQLite database on Android with this method:



public void addMedicine(Medicine medicine)
{
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(COLUMN_NAME, medicine.get_name());
values.put(COLUMN_FORMAT, medicine.get_format());
values.put(COLUMN_AMOUNT, medicine.get_amount());
values.put(COLUMN_EXP_DATE, medicine.get_exp_date());
values.put(COLUMN_TIME, medicine.get_time());

db.insert(TABLE_MEDICINES, null, values);

db.close();
}


The Medicine object i pass to the addMedicine method is an instance of a class i created, and it works.


When i want to select data from the TABLE_MEDICINES i use this function:



public int getMedicines()
{
String med = "";
SQLiteDatabase db = getWritableDatabase();
String SELECT_MEDICINES = "SELECT * FROM " + TABLE_MEDICINES;

// Setting cursor to the first row.

Cursor cursor = db.rawQuery(SELECT_MEDICINES, null);
cursor.moveToFirst();

int tmp_count = 0;

while(!cursor.isAfterLast())
{
if(cursor.getString(cursor.getColumnIndex("name")) != null)
{
med += cursor.getString(cursor.getColumnIndex("name"));
med += "\n";
tmp_count++;
}
}

db.close();

return tmp_count;

}


I only count how many rows are in the table and set the text of a TextView with the tmp_count value. When i run the app, the TextView is always 0, and i think the database is empty so the row hasn't been inserted. Anyone as any idea? Thank you all in advance!


Aucun commentaire:

Enregistrer un commentaire