SQLite数据库无法打开

时间:2018-08-19 22:32:34

标签: java android sqlite

这是我第一次在此网站上提问,我对sqlite数据库还很陌生。我已经搜索了一个研究过的并且尝试了许多解决方案。老实说,我不知道该怎么办了。请给我一些建议。我首先尝试使用资产助手,但这给了我同样的问题。我认为也许是因为它已经过时并且不再受管理了,所以我不得不在没有资产助手的情况下复制数据库。我仍然遇到同样的问题。路径似乎也是正确的。我已经有清单中的权限。我只是不知道如何确定确切的问题。     包com.masbubulkarim.collegehunt;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v4.content.ContextCompat;
import android.util.Log;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;


public class DatabaseHelper extends SQLiteOpenHelper {
    private static Context mContext;
    private static final String TAG = "DatabaseHelper";
    private static final String DB_NAME = "collegeDataBase.db";
    private String DB_PATH;
    private static final int DB_VER = 1;
    private SQLiteDatabase myDataBase;

    public DatabaseHelper(Context context) {
        super(mContext, DB_NAME, null, DB_VER);
        this.mContext = context;
        DB_PATH = mContext.getDatabasePath(DB_NAME).getPath();
        Log.d(TAG, "DatabaseHelper: path: " + DB_PATH);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void prepareDataBase(){
        //check if database is already in a specified path or not
        boolean exists = checkDataBase();

        if(exists){
            Log.d(TAG, "prepareDataBase: database exists...now setting up");
            try{
                copyDataBase();
            }catch(IOException e){
                Log.e(TAG, "prepareDataBase" + e.getMessage());
            }
        } else {
            Log.e(TAG, "prepareDataBase: Database does not exist...now setting up");
            try{
                copyDataBase();
            }catch(IOException e){
                Log.e(TAG, "prepareDataBase" + e.getMessage());
            }
        }
        openDatabase();
    }

    //method to see if database exists or not
    private boolean checkDataBase() throws SQLException{
        boolean exists = false;
        try{
            File file = new File(DB_PATH);
            exists = file.exists();
            if(!exists){
                file.mkdirs();
            }
        }catch(SQLException e){
            Log.e(TAG, "chekDataBase:" + e.getMessage());
        }
        return exists;
    }

    private void copyDataBase() throws IOException{
        OutputStream outputStream = new FileOutputStream(DB_PATH);
        InputStream inputStream = mContext.getAssets().open("database/" + DB_NAME);

        byte[] buffer = new byte[1024];
        int length;
        while((length = inputStream.read(buffer)) > 0){
            outputStream.write(buffer, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

    private void openDatabase(){
        myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
    }

    public List<College> getCollegesByLoc(){
        Log.d(TAG, "getCollegesByLoc: setting up colleges by location");
        openDatabase();
        List<College> list = new ArrayList<>();
        return list;
    }
}

这是日志

08-19 18:13:40.728 30793-30793/com.masbubulkarim.collegehunt E/SQLiteDatabase: Failed to open database '/data/data/com.masbubulkarim.collegehunt/databases/collegeDataBase.db'.
    android.database.sqlite.SQLiteDiskIOException: unknown error (code 10): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
        at com.masbubulkarim.collegehunt.DatabaseHelper.openDatabase(DatabaseHelper.java:100)
        at com.masbubulkarim.collegehunt.DatabaseHelper.prepareDataBase(DatabaseHelper.java:67)
        at com.masbubulkarim.collegehunt.Map.onCreate(Map.java:89)
        at android.app.Activity.performCreate(Activity.java:6010)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        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:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
08-19 18:13:40.729 30793-30793/com.masbubulkarim.collegehunt E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.masbubulkarim.collegehunt, PID: 30793
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.masbubulkarim.collegehunt/com.masbubulkarim.collegehunt.Map}: android.database.sqlite.SQLiteDiskIOException: unknown error (code 10): Could not open database
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
        at android.app.ActivityThread.access$800(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        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:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: android.database.sqlite.SQLiteDiskIOException: unknown error (code 10): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
        at com.masbubulkarim.collegehunt.DatabaseHelper.openDatabase(DatabaseHelper.java:100)
        at com.masbubulkarim.collegehunt.DatabaseHelper.prepareDataBase(DatabaseHelper.java:67)
        at com.masbubulkarim.collegehunt.Map.onCreate(Map.java:89)
        at android.app.Activity.performCreate(Activity.java:6010)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
        at android.app.ActivityThread.access$800(ActivityThread.java:155) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5343) 
        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:905) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

1 个答案:

答案 0 :(得分:0)

尝试使用getAbsolutePath()而不是getPath()。