在android中的sqlite数据库中获取错误

时间:2013-09-03 09:56:24

标签: android sqlite

09-03 10:53:15.349: E/SQLiteLog(25173): (14) cannot open file at line 30191 of [00bb9c9ce4]
09-03 10:53:15.349: E/SQLiteLog(25173): (14) os_unix.c:30191: (2) open(/data/data/uk.org.nt.android.app1/databases/ntdb) - 
09-03 10:53:15.359: E/SQLiteDatabase(25173): Failed to open database '/data/data/uk.org.nt.android.app1/databases/ntdb'.
09-03 10:53:15.359: E/SQLiteDatabase(25173): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at uk.org.nt.android.dynamic.dao.HandbookDBHelper.getAttractionsFromDB(HandbookDBHelper.java:152)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at uk.org.nt.android.dynamic.dao.HandbookDBHelper.findAttractionsForDeletion(HandbookDBHelper.java:96)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at uk.org.nt.android.service.UpdateService.performDeletions(UpdateService.java:270)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at uk.org.nt.android.service.UpdateService.handleHttpResponse(UpdateService.java:138)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at uk.org.nt.android.dynamic.http.HTTPSHelper.onPostExecute(HTTPSHelper.java:91)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at uk.org.nt.android.dynamic.http.HTTPSHelper.onPostExecute(HTTPSHelper.java:1)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.os.AsyncTask.finish(AsyncTask.java:631)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-03 10:53:15.359: E/SQLiteDatabase(25173):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)

public class HandbookDBHelper extends SQLiteOpenHelper {
    public static final int REALM = 1; // handbook
    private static final String LOG_TAG = "HandbookDBHelper";
    // name of the database file for your application -- change to something
    // appropriate for your app
    // private static final String DATABASE_NAME = "ntdb";
    // any time you make changes to your database objects, you may have to
    // increase the database version
    private static final int DATABASE_VERSION = 1;

    File dbFile = null;

    private static String DATABASE_NAME = "ntdb";

    Context context;

    static SimpleDateFormat pricesFormatter = new SimpleDateFormat("yyyy-MM-dd");

    static final long EPOCH_DIFF = 978307200;

    public HandbookDBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1); // set db version

        this.context = context;

        String fullDBName = context.getDatabasePath(DATABASE_NAME)
                .getAbsolutePath();
        dbFile = new File(fullDBName);
    }

    public void unpackDatabase() throws IOException {

        OutputStream databaseOutputStream = new FileOutputStream(dbFile);
        InputStream databaseInputStream;

        byte[] buffer = new byte[1024];
        int partSuffix = 0;
        int dbId;
        String dbPartPrefix = "partdb";
        long bytesWritten = 0;
        while ((dbId = context.getResources().getIdentifier(
                dbPartPrefix + partSuffix, "raw", context.getPackageName())) != 0) {
            databaseInputStream = context.getResources().openRawResource(dbId);

            while ((databaseInputStream.read(buffer)) > 0) {
                databaseOutputStream.write(buffer);
                bytesWritten += 1024;
            }
            databaseInputStream.close();
            partSuffix++;
        }
        databaseOutputStream.flush();
        databaseOutputStream.close();

    }

    /**
     * where an attraction exists on the database but does not exist in the
     * incomming list.
     * 
     */
    public Map<String, HandbookAttraction> findAttractionsForDeletion(
            HandbookAttractions handbookAttractions) {
        Map<String, HandbookAttraction> attractionsMap = getAttractionsFromDB();

        for (HandbookAttraction handbookAttraction : handbookAttractions
                .getHandbookAttractionsList()) {
            if (attractionsMap.get(handbookAttraction.getUUID()) != null) {
                attractionsMap.remove(handbookAttraction.getUUID());
            }
        }
        return attractionsMap;
    }

    /**
     * 
     * @param attractionsList
     * @return
     */
    public Map<String, HandbookAttraction> findAttractionsForUpdate(
            HandbookAttractions handbookAttractions) {

        Map<String, HandbookAttraction> attractionsMap = getAttractionsFromDB();

        // compare versions
        Log.d(LOG_TAG, "comparing versions");
        for (HandbookAttraction handbookAttraction : handbookAttractions
                .getHandbookAttractionsList()) {
            HandbookAttraction dbAttraction = attractionsMap
                    .get(handbookAttraction.getUUID());
            if (dbAttraction == null) {
                Log.d(LOG_TAG, "NEW: " + handbookAttraction.getName());
            } else if (dbAttraction.getVersion() < handbookAttraction
                    .getVersion()) {
                Log.d(LOG_TAG, "UPDATE: " + handbookAttraction.getName()
                        + " from " + dbAttraction.getVersion() + " to "
                        + handbookAttraction.getVersion());
            } else {
                Log.d(LOG_TAG, "NO CHANGE: " + handbookAttraction.getName()
                        + " was " + dbAttraction.getVersion() + " now "
                        + handbookAttraction.getVersion());

                attractionsMap.remove(dbAttraction.getUUID());
            }
        }
        Log.d(LOG_TAG, "total number of additions or changes :"
                + attractionsMap.size());
        // attractionsMap now contains attractions requiring update or addition.

        return attractionsMap;
    }

    /**
     * get the list of attractions from the DB and return them in a map keyed by
     * UUID
     * 
     * @return
     */
    public Map<String, HandbookAttraction> getAttractionsFromDB() {
        SQLiteDatabase attractionsDB = SQLiteDatabase.openDatabase(
                dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);

        Map<String, HandbookAttraction> attractionsMap = new Hashtable<String, HandbookAttraction>();

        Cursor cursor = attractionsDB.rawQuery(
                "select _id, ZVERSION, ZUUID from ZATTRACTION", null);
        int cpos = 0;
        cursor.moveToFirst();
        while (cpos < cursor.getCount()) {
            HandbookAttraction attraction = new HandbookAttraction();
            attraction.setDbId(cursor.getInt(cursor
                    .getColumnIndexOrThrow("_id")));
            attraction.setVersion(cursor.getInt(cursor
                    .getColumnIndexOrThrow("ZVERSION")));
            attraction.setUUID(cursor.getString(cursor
                    .getColumnIndexOrThrow("ZUUID")));

            attractionsMap.put(
                    cursor.getString(cursor.getColumnIndexOrThrow("ZUUID")),
                    attraction);
            cpos++;
            cursor.moveToPosition(cpos);

        }
        cursor.close();

        attractionsDB.close();
        Log.d(LOG_TAG, "loaded " + attractionsMap.size()
                + " attractions from db");

        return attractionsMap;
    }

    public void showDb() {

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    /**
     * perform update / addition for given attraction
     * 
     * @param handbookAttractionDetail
     */
    public void updateAttraction(
            HandbookAttractionDetail handbookAttractionDetail) {
        SQLiteDatabase attractionsDB = SQLiteDatabase.openDatabase(
                dbFile.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE);

        int attractionRowId = clearDownAttraction(handbookAttractionDetail,
                attractionsDB);
        boolean isNewAttraction = (attractionRowId == -1);
        //
        // now re-build
        //
        rebuildAttraction(handbookAttractionDetail, isNewAttraction);
        attractionsDB.close();
    }

1 个答案:

答案 0 :(得分:0)

特别是,有一个openDatabase()命令可以在给定文件路径的情况下访问数据库。

SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);

只需将数据库的路径指定为路径变量,它就可以正常工作。