代码之家  ›  专栏  ›  技术社区  ›  Ujjwal Jung Thapa

外部数据库访问,我的应用程序崩溃

  •  0
  • Ujjwal Jung Thapa  · 技术社区  · 6 年前

    是的,我已经搜索了好几天了。我仍然坚持的问题是 我的应用程序在启动时崩溃 当我试图 访问外部数据库 (通过创建) 用于sqlite的数据库浏览器 “命名为”test.db”)。 唯一的目的当然是 预填充 我的应用程序有一些数据。

    我在Android Studio中保存了资产文件夹中的数据库文件。最后,我实现了一个帮助器类来访问数据库。

    这是帮助程序类:

    public class DataBaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 1;
    private static String DATABASE_PATH = "";
    
    private SQLiteDatabase myDataBase;
    private final Context myContext;
    
    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.myContext = context;
        if (Build.VERSION.SDK_INT >= 17) {
            DATABASE_PATH = context.getApplicationInfo().dataDir + "/databases/";
        } else {
            DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        }
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
    
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
    }
    
    public void createDatabase() throws IOException {
        createDB();
    }
    
    private void createDB() {
        boolean dbExists = checkDatabase();
        if (dbExists) {
            //do nothing
        } else {
            this.getReadableDatabase();
            try {
                copyDatabase();
            } catch (IOException ie) {
                throw new Error("Error copying database");
            }
        }
    }
    
    private boolean checkDatabase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            checkDB.setLocale(Locale.getDefault());
        } catch (SQLiteException e) {
            throw new Error("SQLite exception");
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null;
    }
    
    private void copyDatabase() throws IOException {
        InputStream myInput = null;
        OutputStream myOutput = null;
        String outFileName = DATABASE_PATH + DATABASE_NAME;
    
        try{
            myInput = myContext.getAssets().open(DATABASE_NAME);
            myOutput = new FileOutputStream(outFileName);
    
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }catch (IOException ie){
            throw new Error("Copydatabase run error ");
        }
    
    }
    
    @Override
    public synchronized void close() {
        if(myDataBase != null){
            myDataBase.close();
        }
        super.close();
    }
    }
    

    在主活动中,我刚刚调用了helper类的createDatabase()方法。代码如下:

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        DataBaseHelper myDbHelper = new DataBaseHelper(this);
        try{
            myDbHelper.createDatabase();
        }catch (IOException ioe){
            throw new Error("Unable to CREATE DATABASE");
        }
    }
    }
    

    logcat将问题指示为:

    AndroidRuntime:致命异常:main java.lang.error:sqlite DabaseHelpE.ExkDebug数据库(DabaseEHelp.java:79)的异常 数据库帮助器.CREATEDB(DavaSeasHelp.java:59)AT 数据库帮助器.CREATEATATATABASE(DavaSeaHelp.java:55)AT 主活动.OnCube(MaunActudia. Java:18)

    指向我的checkdatabase()方法。但现在我无法解决这个问题。

    我正在使用android studio 3.0.1,db browser for sqlite 3.10.1,最低API级别使用17。

    请帮忙!

    1 回复  |  直到 6 年前
        1
  •  0
  •   mehul chauhan    6 年前

    试试这个

     public static final String DATABASE_NAME = "abc.db";
     public final static String DATABASE_PATH = "/data/data/"+ PACKAGE_NAME + 
     "/databases/";
     public static final int DATABASE_VERSION = 1;
    
    
      public void createDatabase()  {
        boolean dbExist1 = checkDataBase();
        if (!dbExist1) {
            this.getReadableDatabase();
            try {
                this.close();
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }
    
    // Check database already exist or not
    private boolean checkDataBase() {
        boolean checkDB = false;
        try {
            String myPath = DATABASE_PATH + DATABASE_NAME;
            File dbfile = new File(myPath);
            checkDB = dbfile.exists();
        } catch (SQLiteException e) {
        }
        return checkDB;
    }
    
    // Copies your database from your local assets-folder to the just created
    // empty database in the system folder
    public void copyDataBase() throws IOException {
        String outFileName = DATABASE_PATH + DATABASE_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
    
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();
    }