Android– 无法打开数据库

Android– 无法打开数据库,第1张

概述我在Asset文件夹中有我的db.sqlite文件,当我尝试打开它时出现此错误:android.database.sqlite.SQLiteCantOpenDatabaseException:unknownerror(code14):Couldnotopendatabase我尝试了不同的路径和名称(有和没有扩展名),但我不能使它工作.packagecom.example.APP.

我在Asset文件夹中有我的db.sqlite文件,当我尝试打开它时出现此错误:

android.database.sqlite.sqliteCantopenDatabaseException: unkNown
error (code 14): Could not open database

我尝试了不同的路径和名称(有和没有扩展名),但我不能使它工作.

package com.example.APP.db;import java.io.file;import java.io.fileOutputStream;import java.io.IOException;import java.io.@R_404_5983@Stream;import java.io.OutputStream;import androID.content.Context;import androID.database.sqlException;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteException;import androID.database.sqlite.sqliteOpenHelper;import androID.Widget.Toast;public class DataBaseHelper extends sqliteOpenHelper {// The AndroID's default system path of your application database.private static String DB_PATH = "/data/data/com.example.APP/databases/";private static String DB_name = "app";private sqliteDatabase myDataBase;private final Context myContext;/** * Constructor Takes and keeps a reference of the passed context in order to * access to the application assets and resources. *  * @param context */public DataBaseHelper(Context context) {    super(context, DB_name, null, 1);    this.myContext = context;}/** * Creates a empty database on the system and rewrites it with your own * database. * */public voID createDataBase() throws IOException {    boolean dbExist = checkDataBase();    if (dbExist) {        // do nothing - database already exist    } else {        // By calling this method and empty database will be created into        // the default system path        // of your application so we are gonna be able to overwrite that        // database with our database.        this.getReadableDatabase();        try {            copyDataBase();        } catch (IOException e) {            throw new Error("Error copying database");        }    }}/** * Check if the database already exist to avoID re-copying the file each * time you open the application. *  * @return true if it exists, false if it doesn't */ private boolean checkDataBase(){        boolean checkdb = false;        try{            String myPath = myContext.getfilesDir().getabsolutePath().replace("files", "databases")+file.separator + DB_name;            file dbfile = new file(myPath);                            checkdb = dbfile.exists();        }        catch(sqliteException e){            System.out.println("Database doesn't exist");        }        return checkdb;    }/** * copIEs your database from your local assets-folder to the just created * empty database in the system folder, from where it can be accessed and * handled. This is done by transfering bytestream. * */private voID copyDataBase() throws IOException {    // Open your local db as the @R_404_5983@ stream    @R_404_5983@Stream my@R_404_5983@ = myContext.getAssets().open(DB_name);    // Path to the just created empty db    String outfilename = DB_PATH + DB_name;    // Open the empty db as the output stream    OutputStream myOutput = new fileOutputStream(outfilename);    // transfer bytes from the @R_404_5983@file to the outputfile    byte[] buffer = new byte[1024];    int length;    while ((length = my@R_404_5983@.read(buffer)) > 0) {        myOutput.write(buffer, 0, length);    }    // Close the streams    myOutput.flush();    myOutput.close();    my@R_404_5983@.close();} public voID openDataBase() throws sqlException{    //Open the database    String myPath = DB_PATH + DB_name;    myDataBase = sqliteDatabase.openDatabase(myPath, null, sqliteDatabase.OPEN_Readonly);    }@OverrIDepublic synchronized voID close() {    if (myDataBase != null)        myDataBase.close();    super.close();}@OverrIDepublic voID onCreate(sqliteDatabase db) {}@OverrIDepublic voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {}}

我在这里尝试打开时收到错误:

package com.example.APP;import androID.app.Activity;import androID.os.Bundle;import androID.Widget.TextVIEw;import com.example.APP.db.DataBaseHelper;public class SpeakersListActivity extends Activity {public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.mylayout);          DataBaseHelper dbHelper = new DataBaseHelper(this);    dbHelper.openDataBase();}}

解决方法:

你需要首先检查数据库文件是否存在,而不是在这里

DataBaseHelper dbHelper = new DataBaseHelper(this);dbHelper.openDataBase();

你只是创建一个DataBaseHelper的对象,然后调用.openDataBase()

试试这个构造函数:

public DataBaseHelper(Context context) {    super(context, DB_name, null, 1);   this.myContext = context;   try{        String myPath = DB_PATH + DB_name; // also check the extension of you db file         file dbfile = new file(myPath);                            if( dbfile.exists());             Toast.makeText(context, "database exists", Toast.LENGTH_LONG).show();            else             Toast.makeText(context, "cant find database", Toast.LENGTH_LONG).show();        }        catch(sqliteException e){            System.out.println("Database doesn't exist");        }}
总结

以上是内存溢出为你收集整理的Android – 无法打开数据库全部内容,希望文章能够帮你解决Android – 无法打开数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1106892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存