lundi 1 février 2016

Android/SQLite: "file unlinked while open" error despite using getWritableDatabase

I've been climbing up the walls over this thing for hours.

My Android app calls getWritableDatabase(), and uses the created database object to perform a series of inserts.

However, even though I'm not closing the SQLiteDatabase object, I'm getting the error

file unlinked while open

and the very untrue exception

attempt to write a readonly database

I could not find any reason for this anywhere!

I'm attaching an abridged version of the code. Thanks for anyone who can help...

public static void syncTables(ArrayList<String> tableNames) {
        // DatabaseHelper extands SQLiteOpenHelper
        final SQLiteDatabase db = DatabaseHelper.getInstance().getWritableDatabase();
        try {
            for (String tableName: tableNames) {
                syncTable(db, tableName);
            }

        }
        catch (Exception ex) {
            // TODO: prevent users from using the app in this inconsistent state
            FileLogger.log(String.format("Error: %s", ex.getMessage()));
        }
        finally {
            if (db != null && db.isOpen()) {
                db.close();
            }
        }
    }

private static void syncTable(SQLiteDatabase db,
                                  String tableName) throws Exception {
        ParseQuery<ParseObject> query = getParseQuery(tableName, 0);

        // do search
        int numItemsToDownload = query.count();

        int numItemsDownloaded = 0;
        if (numItemsToDownload > 0) {
            query.setLimit(1000);
            List<ParseObject> objects = query.find();
            while (numItemsDownloaded < numItemsToDownload) {
                numItemsDownloaded += objects.size();
                int numInserted = 0;
                if (objects.size() > 0) {
                    for (ParseObject obj : objects) {
                        // read table-specific properties
                        ContentValues contentValues = new ContentValues();
                        // IParseObjectReader is our own interface for objects convert Parse objects to our own models
                        IParseObjectReader reader = getObjectReader(tableName);
                        if (reader != null) {
                            reader.readObjectProperties(obj, contentValues);
                        }
                        // THIS IS WHERE I GET THE ERROR
                        db.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
                        numInserted++;
                    }
                }
                if (numItemsDownloaded < numItemsToDownload) {
                    // readOnlyQuery again until numItemsToDownload = numItemsDownloaded
                    query.setSkip(numItemsDownloaded);
                    objects = query.find();
                }
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire