From 1397d4b8682f60e03c641020a0e239a22ef4fe69 Mon Sep 17 00:00:00 2001 From: jacques Date: Tue, 7 Jul 2020 21:36:34 +0200 Subject: [PATCH] fix typo and alignment; fix bug with cursor and getDrugByCIP13 --- .../java/net/foucry/pilldroid/DBMedoc.java | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DBMedoc.java b/app/src/main/java/net/foucry/pilldroid/DBMedoc.java index 89c5233..771763b 100644 --- a/app/src/main/java/net/foucry/pilldroid/DBMedoc.java +++ b/app/src/main/java/net/foucry/pilldroid/DBMedoc.java @@ -17,7 +17,7 @@ import java.io.OutputStream; /** * Created by jfoucry on 5/25/16. */ -class DBMedoc extends SQLiteOpenHelper{ +class DBMedoc extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; @@ -25,12 +25,12 @@ class DBMedoc extends SQLiteOpenHelper{ private final Context myContext; private SQLiteDatabase myDataBase; - private static final String TABLE_NAME = "medicaments"; - private static final String MEDOC_CIS = "cis"; + private static final String TABLE_NAME = "medicaments"; + private static final String MEDOC_CIS = "cis"; private static final String MEDOC_CIP13 = "cip13"; private static final String MEDOC_ADMIN = "mode_administration"; - private static final String MEDOC_NOM = "nom"; - private static final String MEDOC_PRES = "presentation"; + private static final String MEDOC_NOM = "nom"; + private static final String MEDOC_PRES = "presentation"; private static final String[] COLUMNS_NAMES = {MEDOC_CIS, MEDOC_CIP13, MEDOC_ADMIN, MEDOC_NOM, MEDOC_PRES}; @@ -52,11 +52,11 @@ class DBMedoc extends SQLiteOpenHelper{ copyDatabase(dbFile.getPath()); } - return super.getWritableDatabase(); + return super.getWritableDatabase(); } @Override - public synchronized SQLiteDatabase getReadableDatabase() { + public synchronized SQLiteDatabase getReadableDatabase() { File dbFile = myContext.getDatabasePath(dbName); if (dbFile.exists()) return super.getReadableDatabase(); @@ -67,10 +67,12 @@ class DBMedoc extends SQLiteOpenHelper{ } @Override - public void onCreate(SQLiteDatabase db) {} + public void onCreate(SQLiteDatabase db) { + } @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + } private void copyDatabase(String dbPath) { Log.d(TAG, "try to copy database"); @@ -81,7 +83,7 @@ class DBMedoc extends SQLiteOpenHelper{ byte[] buffer = new byte[1024]; int length; while ((length = assetDB.read(buffer)) > 0) { - appDB.write(buffer,0, length); + appDB.write(buffer, 0, length); } appDB.flush(); @@ -108,6 +110,7 @@ class DBMedoc extends SQLiteOpenHelper{ /** * Lookup in the DB for a record corresponding to cpi1 + * * @param cip13 string representing the object we're looking for * @return return a medicament object */ @@ -117,44 +120,40 @@ class DBMedoc extends SQLiteOpenHelper{ SQLiteDatabase db = this.getReadableDatabase(); // Build query - Cursor cursor = db.query(TABLE_NAME, // Which table + /*Cursor cursor = db.query(TABLE_NAME, // Which table COLUMNS_NAMES, // column names " cip13 =?", // selections new String[]{cip13}, // selections args null, // group by null, // having null, // order by - null); // limits + null); // limits*/ + Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " where cip13 = " + cip13, null); + if (cursor.getCount() != 0) { - if (cursor != null) - cursor.moveToFirst(); + // Build medicament object + Medicament medicament = new Medicament(); + // medicament.setId(Integer.parseInt(cursor.getString(0))); + medicament.setCis(cursor.getString(0)); + medicament.setCip13(cursor.getString(1)); + medicament.setMode_administration(cursor.getString(2)); + medicament.setNom(cursor.getString(3)); + medicament.setPresentation(cursor.getString(4)); - // Build medicament object - Medicament medicament = new Medicament(); - // medicament.setId(Integer.parseInt(cursor.getString(0))); - assert cursor != null; - medicament.setCis(cursor.getString(0)); - medicament.setCip13(cursor.getString(1)); - medicament.setMode_administration(cursor.getString(2)); - medicament.setNom(cursor.getString(3)); - medicament.setPresentation(cursor.getString(4)); - /*medicament.setStock(Double.parseDouble(cursor.getString(5))); - medicament.setPrise(Double.parseDouble(cursor.getString(6))); - medicament.setWarnThreshold(Integer.parseInt(cursor.getString(7))); - medicament.setAlertThreshold(Integer.parseInt(cursor.getString(8)));*/ + // Set default values + medicament.setStock(0); + medicament.setPrise(0); + medicament.setWarnThreshold(14); + medicament.setAlertThreshold(7); - // Set default values - medicament.setStock(0); - medicament.setPrise(0); - medicament.setWarnThreshold(14); - medicament.setAlertThreshold(7); + // Log + Log.d(TAG, "getDrug(" + cip13 + ")" + medicament.toString()); - // Log - Log.d(TAG, "getDrug(" + cip13 + ")" + medicament.toString()); + // Return medicament - // Return medicament - - cursor.close(); - return medicament; + cursor.close(); + return medicament; + } else + return null; } }