mercredi 2 mars 2016

Android sqlite database crashes after rapidly querying every x second using timer

I have a project where I get some data from my webservice and then save it to my SQLite db whenever a new set of data arrives. It seems normal at the first few seconds but eventually, it will crash and gives me this error:

03-02 21:52:49.421 5963-5963/com.example.rendell.matt E/SQLiteLog: (14)     
cannot open file at line 30052 of [b3bb660af9]
03-02 21:52:49.421 5963-5963/com.example.rendell.matt E/SQLiteLog: (14)   
os_unix.c:30052: (24) open
(/data/data/http://ift.tt/1L5KCgw) - 
03-02 21:52:49.421 5963-5963/com.example.rendell.matt E/SQLiteLog: (14)  
cannot open file at line 30052 of [b3bb660af9]
03-02 21:52:49.421 5963-5963/com.example.rendell.matt E/SQLiteLog: (14) 
os_unix.c:30052: (24) open
(/data/data/http://ift.tt/1L5KCgw) - 
03-02 21:52:49.421 5963-5963/com.example.rendell.matt E/SQLiteLog: (14) 
statement aborts at 16: [SELECT  * FROM notify WHERE name='sleep' AND    
read=0] unable to open database file
03-02 21:52:49.433 5963-5963/com.example.rendell.matt E/SQLiteQuery: 
exception: unable to open database file (code 14); query: SELECT  * FROM 
notify WHERE name='sleep' AND read=0
03-02 21:52:49.433 5963-5963/com.example.rendell.matt D/AndroidRuntime: 
Shutting down VM
03-02 21:52:49.440 5963-5963/com.example.rendell.matt E/AndroidRuntime:  
FATAL EXCEPTION: main
                                                                    Process: com.example.rendell.matt, PID: 5963
                                                                    android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
                                                                        at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
                                                                        at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
                                                                        at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
                                                                        at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
                                                                        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
                                                                        at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
                                                                        at com.example.rendell.matt.DBHandler.getSleepUnread(DBHandler.java:177)
                                                                        at com.example.rendell.matt.HomeActivity$1$1.run(HomeActivity.java:183)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5253)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

DBHandler.java

public class DBHandler extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "notification";

// Contacts table name
private static final String TABLE_NOTIFICATION = "notify";

// Shops Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TASKID = "taskid";
private static final String KEY_NAME = "name";
private static final String KEY_TIME = "time";
private static final String KEY_READ = "read";

public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NOTIFICATION + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TASKID + " INTEGER," + KEY_NAME + " TEXT," + KEY_TIME + " TEXT,"
            + KEY_READ + " INTEGER" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATION);
    // Creating tables again
    onCreate(db);
}

// Adding new shop
public void addNotification(NotificationTableModel notification) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TASKID, notification.getTaskid());
    values.put(KEY_NAME, notification.getName()); // Shop Name
    values.put(KEY_TIME, notification.getTime()); // Shop Phone Number
    values.put(KEY_READ, notification.getRead());

    // Inserting Row
    db.insert(TABLE_NOTIFICATION, null, values);
    db.close(); // Closing database connection
}

public void inserIfNotExist(NotificationTableModel notification) {

    String selectQuery = "SELECT * FROM " + TABLE_NOTIFICATION + " WHERE taskid=" + notification.getTaskid();

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (!cursor.moveToFirst()) {
        ContentValues values = new ContentValues();
        values.put(KEY_TASKID, notification.getTaskid());
        values.put(KEY_NAME, notification.getName()); // Shop Name
        values.put(KEY_TIME, notification.getTime()); // Shop Phone Number
        values.put(KEY_READ, notification.getRead());

        // Inserting Row
        db.insert(TABLE_NOTIFICATION, null, values);
        db.close(); // Closing database connection
    }
}

// Getting All Shops
public List<NotificationTableModel> getAllShops() {
    List<NotificationTableModel> shopList = new ArrayList<NotificationTableModel>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NOTIFICATION;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            NotificationTableModel shop = new NotificationTableModel();
            shop.setId(Integer.parseInt(cursor.getString(0)));
            shop.setTaskid(Integer.parseInt(cursor.getString(1)));
            shop.setName(cursor.getString(2));
            shop.setTime(cursor.getString(3));
            shop.setRead(Integer.parseInt(cursor.getString(4)));
            // Adding contact to list
            shopList.add(shop);
        } while (cursor.moveToNext());
    }

    // return contact list
    return shopList;
}


public List<NotificationTableModel> getSingleNotify() {
    List<NotificationTableModel> shopList = new ArrayList<NotificationTableModel>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NOTIFICATION + " WHERE time = '07:35 AM'";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            NotificationTableModel shop = new NotificationTableModel();
            shop.setId(Integer.parseInt(cursor.getString(0)));
            shop.setTaskid(Integer.parseInt(cursor.getString(1)));
            shop.setName(cursor.getString(2));
            shop.setTime(cursor.getString(3));
            shop.setRead(Integer.parseInt(cursor.getString(4)));
            // Adding contact to list
            shopList.add(shop);
        } while (cursor.moveToNext());
    }

    // return contact list
    return shopList;
}

// Getting shops Count
public int getShopsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTIFICATION;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    //cursor.close();

    // return count
    return cursor.getCount();
}

public int getFeedUnread() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTIFICATION + " WHERE name='feed' AND read=0";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    //cursor.close();

    // return count
    return cursor.getCount();
}

public int getPlayUnread() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTIFICATION + " WHERE name='stroll' OR name='play' AND read=0";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    //cursor.close();

    // return count
    return cursor.getCount();
}

public int getSleepUnread() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTIFICATION + " WHERE name='sleep' AND read=0";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    //cursor.close();

    // return count
    return cursor.getCount();
}

public int getChangeUnread() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTIFICATION + " WHERE name='change' OR name='bath' AND read=0";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    //cursor.close();

    // return count
    return cursor.getCount();
}

// Updating a shop
public int updateShop(NotificationTableModel notification) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TASKID, notification.getTaskid());
    values.put(KEY_NAME, notification.getName()); // Shop Name
    values.put(KEY_TIME, notification.getTime()); // Shop Phone Number
    values.put(KEY_READ, notification.getRead());

    // updating row
    return db.update(TABLE_NOTIFICATION, values, KEY_ID + " = ?",
            new String[]{String.valueOf(notification.getId())});
}

// Updating a shop
public int updateRead(NotificationTableModel notification) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_READ, 1);

    // updating row
    return db.update(TABLE_NOTIFICATION, values, KEY_ID + " = ?",
            new String[]{String.valueOf(1)});
}

// Deleting a shop
public void deleteShop(NotificationTableModel shop) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NOTIFICATION, KEY_ID + " = ?",
            new String[]{String.valueOf(shop.getId())});
    db.close();
}

public void deleteAll(NotificationTableModel shop) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from " + TABLE_NOTIFICATION);
    db.close();
    }
}

Aucun commentaire:

Enregistrer un commentaire