android - 无法将我的数据库文件访问到应用程序中

标签 android database

我创建了应用程序,我只需要访问 Assets 文件夹中的数据。我正在使用数据库助手类打开数据库,但我无法访问数据库,我正在放置我的代码

public class DidUMean extends Activity {
DataBaseHelper myDbHelper;
public SQLiteDatabase mydatabase;
String searchWord;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.didumean);

    myDbHelper = new DataBaseHelper(DidUMean.this);

    didYouMean(searchWord);

}

public void didYouMean(String searchWord) {
    myDbHelper.openDataBase();
    mydatabase = myDbHelper.getWritableDatabase();
    Cursor cc = mydatabase.rawQuery("SELECT COUNT(*) FROM tblVocab",
            null);
    try {
        if (cc.getCount() == 0) {
            System.out.println("----> "+cc.getCount());
        }
    } catch (SQLException sqle) {
        System.out.println(sqle);
        throw sqle;
    } finally {
        myDbHelper.close();
    }
}
}

我在 Logcat 中遇到以下错误。

03-28 15:12:48.945: ERROR/AndroidRuntime(1995): FATAL EXCEPTION: main
03-28 15:12:48.945: ERROR/AndroidRuntime(1995): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lauruss.verb2verbe/com.lauruss.verb2verbe.DidUMean}: android.database.sqlite.SQLiteException: unable to open database file
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.os.Looper.loop(Looper.java:123)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at java.lang.reflect.Method.invoke(Method.java:521)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at dalvik.system.NativeStart.main(Native Method)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995): Caused by: android.database.sqlite.SQLiteException: unable to open database file
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at com.lauruss.verb2verbe.DataBaseHelper.openDataBase(DataBaseHelper.java:109)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at com.lauruss.verb2verbe.DidUMean.didYouMean(DidUMean.java:27)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at com.lauruss.verb2verbe.DidUMean.onCreate(DidUMean.java:22)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-28 15:12:48.945: ERROR/AndroidRuntime(1995):     ... 11 more

最佳答案

public class DataBaseHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.lauruss.verb2verbe/databases/";
private static String DB_NAME = "v2v.db";
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;
}   

/** * 在系统上创建一个空数据库并用您自己的数据库重写它。 * */ public void createDataBase() 抛出 IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        Log.d("Rikta", "Database Exist");
        //do nothing - database already exist

        }else{
            Log.d("Rikta", "Database Does Not Exist");
            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(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        Log.d("Maulik", myPath);
    //  checkDB = SQLiteDatabase.openOrCreateDatabase(myPath, null);
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        //database does't exist yet.
    }
    if(checkDB != null){
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

/**
 * 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 input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;
    Log.d("copyDataBase", outFileName);
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    Log.d("openDatabase", myPath);
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
 }

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

@Override
public void onCreate(SQLiteDatabase db) {
}

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

    // Add your public helper methods to access and get content from the database.
   // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
   // to you to create adapters for your views.

关于android - 无法将我的数据库文件访问到应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9905087/

相关文章:

android - ListView 或 2 个 ListView 中的 GridView 更好?

android - 不存在导航/状态栏触摸监听器,为什么?

sql - 如何使用 SQL*Plus 从 Oracle 复制到 Sybase?

database - 自动更正打开的查询和数据库连接

mysql - 将数据增长到另一个表 - 数据库设计

java - Lucene 查询结果作为另一个 Lucene 语句的条件

mysql - 使用 mysql 的存储引擎中的存储 28 错误

android - 单击通知栏时复制应用程序

java - 如何将可绘制文件夹中的图像设置为布局而不是字符串 url

android - 如何禁用 ANDROID 以及在 Phonegap 应用程序中长按屏幕后出现的 IOS 的复制和粘贴文本功能