Sqlite Android - 无法复制我的DB使用它

时间:2016-07-07 06:49:53

标签: android sqlite

我在我的Android应用程序中使用Sqlite数据库,这是我第一次遇到相同的错误,我关闭了我的项目和我的模拟器好几天,然后它在那之后工作但仍然不知道为什么, 在那之后,同样的错误又回来了,即使我没有改变程序,这是我的帮手:

String DB_PATH = null;
    private static String DB_NAME = "DBs";
    private SQLiteDatabase myDataBase;
    private final Context myContext;


    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 10);
        this.myContext = context;
        this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
        Log.e("Path 1", DB_PATH);
    }




    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {
            Log.e("YDB", "existOne");
        } else {
            this.getWritableDatabase(); 
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_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 = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[10];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

我从主要的那个叫它:

DataBaseHelper myDbHelper = new DataBaseHelper(MainActivity.this);

        try {
            myDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            myDbHelper.openDataBase();
        } catch (SQLException sqle) {
            throw sqle;
        }

我尝试卸载我的应用程序,这是应用程序不存在时的错误,我们运行:

07-07 06:39:32.120: I/SqliteDatabaseCpp(3077): sqlite returned: error code = 14, msg = cannot open file at line 27701 of [8609a15dfa], db=/data/data/com.up.ridechaser/databases/DBs
07-07 06:39:32.120: I/SqliteDatabaseCpp(3077): sqlite returned: error code = 14, msg = os_unix.c: open() at line 27701 - "" errno=2 path=/data/data/com.up.ridechaser/databases/DBs, db=/data/data/com.up.ridechaser/databases/DBs
07-07 06:39:32.130: E/SqliteDatabaseCpp(3077): sqlite3_open_v2("/data/data/com.up.ridechaser/databases/DBs", &handle, 1, NULL) failed
07-07 06:39:32.140: E/SQLiteDatabase(3077): Failed to open the database. closing it.
07-07 06:39:32.140: E/SQLiteDatabase(3077): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at com.up.ridechaser.DataBaseHelper.checkDataBase(DataBaseHelper.java:53)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at com.up.ridechaser.DataBaseHelper.createDataBase(DataBaseHelper.java:36)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at com.up.ridechaser.MainActivity.onCreate(MainActivity.java:35)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.app.Activity.performCreate(Activity.java:4466)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-07 06:39:32.140: E/SQLiteDatabase(3077):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

以及它存在时的错误:

07-07 06:48:10.821: E/AndroidRuntime(3268): FATAL EXCEPTION: main
07-07 06:48:10.821: E/AndroidRuntime(3268): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.up.ridechaser/com.up.ridechaser.MainActivity}: android.database.sqlite.SQLiteException: no such table: LINES_ST: , while compiling: select * from LINES_ST
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.os.Looper.loop(Looper.java:137)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at java.lang.reflect.Method.invoke(Method.java:511)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 06:48:10.821: E/AndroidRuntime(3268):     at dalvik.system.NativeStart.main(Native Method)
07-07 06:48:10.821: E/AndroidRuntime(3268): Caused by: android.database.sqlite.SQLiteException: no such table: LINES_ST: , while compiling: select * from LINES_ST
07-07 06:48:10.821: E/AndroidRuntime(3268):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)

找不到表,因为应用程序在内部存储空白数据库中进行了抓取!

请不要告诉我检查资产文件夹,因为程序正常工作,卸载应用程序时发生错误!

0 个答案:

没有答案