vendredi 6 mai 2016

Adding new column in sqlite but get "no such column" error

The code was working until I added a new column "dose5" to the DBadapter class. I added dose5 codes to relevant places in DBadapter

I try uninstall and reinstall the app but still give out "no such column" error.

Error Log

    05-07 00:55:37.705 10293-10293/simonhcm.pediatricdosagecalculator E/SQLiteLog: (1) no such column: dose5
    me: FATAL EXCEPTION: main
    Process: simonhcm.pediatricdosagecalculator, PID: 10293
    android.database.sqlite.SQLiteException: no such column: dose5 (code 1): , while compiling: SELECT _id, drugName, drugOtherName, line1, line2, dosetext1, dosetext2, dosetext3, dosetext4, dose1, dose2, dose3, dose4, dose5, sort FROM custom_drug_table ORDER BY sort ASC
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
    at simonhcm.pediatricdosagecalculator.DBAdapter.getAllData(DBAdapter.java:222)
    at simonhcm.pediatricdosagecalculator.CustomDrugAdapter.getCustomDrugList(CustomDrugAdapter.java:67)
    at simonhcm.pediatricdosagecalculator.CustomDrugAdapter.<init>(CustomDrugAdapter.java:34)
    at simonhcm.pediatricdosagecalculator.Custom.onCreateView(Custom.java:49)

My CustomDrugAdapter,I guess error is from getCustomDrugList() method:

private ArrayList<CustomDrugItem> getCustomDrugList() {
    ArrayList<CustomDrugItem> tempList = new ArrayList<CustomDrugItem>();
    DBAdapter dbAdapter = new DBAdapter(getContext());
    dbAdapter.open();
    Cursor c;
    c = dbAdapter.getAllData();
    if (c.moveToFirst()) {
        do {
            int _rowId = new Integer(Integer.parseInt(c.getString(0)));
            int _sortId;// = new Integer(Integer.parseInt(c.getString(13)));
            _sortId = c.getInt(14);

            CustomDrugItem di = new CustomDrugItem(_rowId,
                    c.getString(1),
                    c.getString(2),
                    c.getString(3),
                    c.getString(4),
                    c.getString(5),
                    c.getString(6),
                    c.getString(7),
                    c.getString(8),
                    c.getString(9),
                    c.getString(10),
                    c.getString(11),
                    c.getString(12),
                    c.getString(13),
                    _sortId
            );
            tempList.add(di);
        } while (c.moveToNext());
    }
    dbAdapter.close();
    return tempList;
}

Finally my DBadapter. I believe the error is around getAllData() method? public class DBAdapter {

    private static final String TAG = "DBAdapter";

    public static final String DATABASE_PATH = "/data/data/simonhcm.pediatricdosagecalculator/databases/";
    public static final String DATABASE_NAME = "custom_drug.sqlite";
    public static final String DATABASE_TABLE = "custom_drug_table";
    public static final int DATABASE_VERSION = 2;
    // The version number must be incremented each time a change to DB structure occurs.

    // Field Names:
    public static final String KEY_ROWID = "_id";
    public static final String KEY_DRUG_NAME = "drugName";
    public static final String KEY_DRUG_OTHER_NAME = "drugOtherName";
    public static final String FREE_TEXT_LINE_1 = "line1";
    public static final String FREE_TEXT_LINE_2 = "line2";
    public static final String DOSE_TEXT_LINE_1 = "dosetext1";
    public static final String DOSE_TEXT_LINE_2 = "dosetext2";
    public static final String DOSE_TEXT_LINE_3 = "dosetext3";
    public static final String DOSE_TEXT_LINE_4 = "dosetext4";
    public static final String DOSE_1 = "dose1";
    public static final String DOSE_2 = "dose2";
    public static final String DOSE_3 = "dose3";
    public static final String DOSE_4 = "dose4";
    public static final String DOSE_5 = "dose5";
    public static final String SORT = "sort";

    public static final String[] ALL_KEYS = new String[]{KEY_ROWID,
            KEY_DRUG_NAME,
            KEY_DRUG_OTHER_NAME,
            FREE_TEXT_LINE_1,
            FREE_TEXT_LINE_2,
            DOSE_TEXT_LINE_1,
            DOSE_TEXT_LINE_2,
            DOSE_TEXT_LINE_3,
            DOSE_TEXT_LINE_4,
            DOSE_1,
            DOSE_2,
            DOSE_3,
            DOSE_4,
            DOSE_5,
            SORT};

    private final Context context;
    private DatabaseHelper DBHelper;
    private static SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        Context helperContext;

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            helperContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, oldVersion + " to " + newVersion
                    + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS contacts");
            onCreate(db);
        }

        public void createDatabase(){
            boolean dbExist = checkDataBase();
            if (dbExist){
            } else {
                this.getReadableDatabase();
                try {
                    copyDataBase();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
            }
        }

        public boolean checkDataBase(){
            SQLiteDatabase checkDB = null;
            try {
                String myPath = DATABASE_PATH + DATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            } catch (SQLiteException e){
            }
            if (checkDB != null){
                checkDB.close();
            }
            return checkDB != null ? true : false;
        }
        private void copyDataBase() throws IOException {
            InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);

            String outFileName = DATABASE_PATH + DATABASE_NAME;

            OutputStream myOutput = new FileOutputStream(outFileName);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0){
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }

        public void openDatabase(){
            String myPath = DATABASE_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        }

        public synchronized void close(){
            if (db != null)
                db.close();
            super.close();
        }
    }



    public DBAdapter open() throws SQLException {
        DBHelper = new DatabaseHelper(context);
        DBHelper.createDatabase();
        DBHelper.openDatabase();
        return this;
    }

    public void close() {
        DBHelper.close();
    }

    public long insertData(String drugName,
                              String drugOtherName,
                              String freetext_line1,
                              String freetext_line2,
                              String dosetext_line1,
                              String dosetext_line2,
                              String dosetext_line3,
                              String dosetext_line4,
                              String dose1,
                              String dose2,
                              String dose3,
                              String dose4,
                           String dose5,
                              int sort) {
        db = DBHelper.getWritableDatabase();
        ContentValues addValues = new ContentValues();
        addValues.put(KEY_DRUG_NAME, drugName);
        addValues.put(KEY_DRUG_OTHER_NAME, drugOtherName);
        addValues.put(FREE_TEXT_LINE_1, freetext_line1);
        addValues.put(FREE_TEXT_LINE_2, freetext_line2);
        addValues.put(DOSE_TEXT_LINE_1, dosetext_line1);
        addValues.put(DOSE_TEXT_LINE_2, dosetext_line2);
        addValues.put(DOSE_TEXT_LINE_3, dosetext_line3);
        addValues.put(DOSE_TEXT_LINE_4, dosetext_line4);
        addValues.put(DOSE_1, dose1);
        addValues.put(DOSE_2, dose2);
        addValues.put(DOSE_3, dose3);
        addValues.put(DOSE_4, dose4);
        addValues.put(DOSE_5, dose5);
        addValues.put(SORT, sort);
//        addValues.put(SORT, getLastRowID());

        Log.d("", String.format("RowId: Sort ID  = %d", sort));
        // .Insert the data into the database.
        return db.insert(DATABASE_TABLE, null, addValues);
    }

    public boolean deleteData(long rowId) {
        db = DBHelper.getWritableDatabase();
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
    public void deleteAll() {
        Cursor c = getAllData();
        long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
        if (c.moveToFirst()) {
            do {
                deleteData(c.getLong((int) rowId));
            } while (c.moveToNext());
        }
        c.close();
    }
    public Cursor getLastRowID(){
        int temp;
        db = DBHelper.getWritableDatabase();
        Cursor mCursor = db.query(DATABASE_TABLE,ALL_KEYS, null, null, null, null, null);
        if (mCursor != null){
            mCursor.moveToLast();
        }
//        if (mCursor.getC)
//        temp = mCursor.getInt(0) + 1;
        return mCursor;
    }

    public Cursor getAllData() {
        db = DBHelper.getWritableDatabase();
        Cursor mCursor = db.query(DATABASE_TABLE, ALL_KEYS, null, null, null, null, SORT + " ASC");
        if (mCursor != null){
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public Cursor getDataWithRow(long rowId) throws SQLException {
        db = DBHelper.getWritableDatabase();
        Cursor mCursor = db.query(true, DATABASE_TABLE, ALL_KEYS, KEY_ROWID + "=" + rowId,
                null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public boolean updateData(long rowId,
                                 String drugName,
                                 String drugOtherName,
                                 String freetext_line1,
                                 String freetext_line2,
                                 String dosetext_line1,
                                 String dosetext_line2,
                                 String dosetext_line3,
                                 String dosetext_line4,
                                 String dose1,
                                 String dose2,
                                 String dose3,
                                 String dose4,
                              String dose5,
                                 long sort) {
        db = DBHelper.getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(KEY_DRUG_NAME, drugName);
        args.put(KEY_DRUG_OTHER_NAME, drugOtherName);
        args.put(FREE_TEXT_LINE_1, freetext_line1);
        args.put(FREE_TEXT_LINE_2, freetext_line2);
        args.put(DOSE_TEXT_LINE_1, dosetext_line1);
        args.put(DOSE_TEXT_LINE_2, dosetext_line2);
        args.put(DOSE_TEXT_LINE_3, dosetext_line3);
        args.put(DOSE_TEXT_LINE_4, dosetext_line4);
        args.put(DOSE_1, dose1);
        args.put(DOSE_2, dose2);
        args.put(DOSE_3, dose3);
        args.put(DOSE_4, dose4);
        args.put(DOSE_5, dose5);
        args.put(SORT, sort);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

Aucun commentaire:

Enregistrer un commentaire