vendredi 12 juin 2015

Android-Not able to write to database

I am having issue with android app connectivity to SQLite database. I have an instance of SQLite database in Assets folder. 'Select' operation returns the data that is manually inserted into database.But, Write operations (Insert,Update,Delete) are failing. I was using 'this.getReadableDatabase()' and replaced with 'this.getWritableDatabase', Similarly, SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY) was replaced with SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE), but still of no use. I am testing on Samsung Galaxy S i-90003 with 2.3.6 version. Some one please help me

    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
   // values.put(KEY_ID,contact.getId());//Contact Id
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_IMAGE, contact.getImage()); // Contact Phone
    // Inserting Row
    try {
        if(isTableExists(TABLE_IMAGES,true)) {
            db.insert(TABLE_IMAGES, null, values);
        }
    }catch (Exception e) {
        System.out.println("Error inserting:"+e.getMessage());
    }
    db.close(); // Closing database connection

   public boolean isTableExists(String tableName, boolean openDb) {
    if(openDb) {
        if(db == null || !db.isOpen()) {
            db = getWritableDatabase();
        }
        if(!db.isReadOnly()) {
            db.close();
            db = getWritableDatabase();
        }
    }
    Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master  where tbl_name = '"+tableName+"'", null);
    if(cursor!=null) {
        if(cursor.getCount()>0) {
            cursor.close();
            return true;
        }
        cursor.close();
    }
    return false;
}

Aucun commentaire:

Enregistrer un commentaire