Невозможно открыть файл базы данных на устройстве (HTC Desire) - PullRequest
1 голос
/ 08 февраля 2011

Я использую некоторый код, который скопирует существующую базу данных SQLite из моей папки ресурсов в мою папку / data / data / package. Я написал код и протестировал его на эмуляторе Android, и он отлично работает. Если я пытаюсь установить его на свое устройство (HTC Desire), я получаю «SQLiteException: невозможно открыть файл базы данных»

Я застрял на этом уже 3 дня и не могу понять, что происходит не так. Я не знаю, нельзя ли открыть базу данных, потому что она не создана, или мне не хватает некоторых разрешений базы данных в файле манифеста. Это мой код DataBaseHelper:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper{
    //private static String DB_PATH = "/data/data/com.drager/databases/";
    private static String DB_PATH = Environment.getDataDirectory()+"/data/com.drager/databases/";
    final static String DB_NAME = "myDBName";
    private SQLiteDatabase myDataBase=null;
    private final Context myContext;
    private DataBaseHelper myDbHelper;
    private static String TAG ="MyActivity";

    public DataBaseHelper(Context context){
        super(context, DB_NAME, null, 1);
        this.myContext = context;

    }

    public DataBaseHelper createDataBase() throws IOException{
        boolean dbExist =checkDataBase();
        //SQLiteDatabase db_read =null;
        Log.i(TAG,"############value of dbExist"+dbExist+"##########");
        //if (dbExist){
            //db must exist
        //}
        //else{
        //myDbHelper = new DataBaseHelper(myContext);
        //myDataBase = myDbHelper.getReadableDatabase();
        //myDataBase.close();
        //this.getReadableDatabase();
            //db_read.close();

        //    try {
                copyDataBase();
        //  } catch (IOException e) {
        //      throw new Error("error copying database");
        //  }
    //  }
        return this;

    }

    public Cursor executeStatement(){
        Log.i(TAG,"in execute statement");
        Cursor cursor=null;

        cursor=myDataBase.rawQuery("SELECT _ID, title, value "+
                    "FROM constants ORDER BY title",
                     null);
        return cursor;
    }

    public String getTextViewItem(){

        Cursor cursor=null;

        String str="";
        //store query results in cursor
        cursor=myDataBase.rawQuery("SELECT description FROM product_details" ,
                     null);
        cursor.moveToNext();
        //cast cursor content from the index of 'description Column as a string
        str =cursor.getString(cursor.getColumnIndex("description"));

        return str;
    }

    public void copyDataBase() throws IOException{
        // open db as input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        //path to newly created db
        String outFileName =DB_PATH + DB_NAME;

        //open empty db as 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);
        }

        myOutPut.flush();
        myOutPut.close();
        myInput.close();


    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        /*
        if (checkDB != null){
            checkDB.close();
        }*/
        return checkDB !=null ? true : false;
    }

    public void openDataBase()throws SQLException{
        //open the database
        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();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

Это мой файл манифеста

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.drager"
 android:versionCode="1"
 android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/

app_name" android:debuggable="true">
  <activity android:name=".Drager"
             android:label="Drager">
<intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Это Logcat

02-09 09:09:15.883: ERROR/AndroidRuntime(8313):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.drager/com.drager.Drager}:
android.database.sqlite.SQLiteException: unable to open database file
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2787)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2803)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.ActivityThread.access$2300(ActivityThread.java:135)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.os.Handler.dispatchMessage(Handler.java:99)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.os.Looper.loop(Looper.java:144)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.ActivityThread.main(ActivityThread.java:4937)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
java.lang.reflect.Method.invokeNative(Native Method)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
java.lang.reflect.Method.invoke(Method.java:521)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
dalvik.system.NativeStart.main(Native Method)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313): Caused by:
android.database.sqlite.SQLiteException: unable to open database file
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:
1902)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:
884)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
com.drager.DataBaseHelper.checkDataBase(DataBaseHelper.java:108)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
com.drager.DataBaseHelper.createDataBase(DataBaseHelper.java:32)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
com.drager.Drager.onCreate(Drager.java:46)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1069)
02-09 09:09:15.883: ERROR/AndroidRuntime(8313):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2751)

Заранее спасибо

1 Ответ

0 голосов
/ 09 февраля 2011

Спасибо IDX, ваш очень красивый мужчина или женщина! Я попробовал то, что вы предложили, немного поиграв, я понял, что попытка / ловля - это ключ. Вот мой исправленный код, надеюсь, он поможет кому-то еще.

public class DataBaseHelper extends SQLiteOpenHelper{
    //private static String DB_PATH = "/data/data/com.drager/databases/";
    private static String DB_PATH = Environment.getDataDirectory()+"/data/com.drager/databases/";
    final static String DB_NAME = "myDBName";
    private SQLiteDatabase myDataBase=null;
    private final Context myContext;
    private DataBaseHelper myDbHelper;
    private static String TAG ="MyActivity";

    public DataBaseHelper(Context context){
        super(context, DB_NAME, null, 1);
        this.myContext = context;

    }

    public DataBaseHelper createDataBase() throws IOException{
        boolean dbExist =checkDataBase();
        //SQLiteDatabase db_read =null;
        Log.i(TAG,"############value of dbExist"+dbExist+"##########");
        if (dbExist){
            //db must exist
        }
        else{
        myDbHelper = new DataBaseHelper(myContext);
        myDataBase = myDbHelper.getReadableDatabase();
        myDataBase.close();
        //this.getReadableDatabase();
            //db_read.close();

            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("error copying database");
            }
        }
        return this;

    }

    public Cursor executeStatement(){
        Log.i(TAG,"in execute statement");
        Cursor cursor=null;

        cursor=myDataBase.rawQuery("SELECT _ID, title, value "+
                    "FROM constants ORDER BY title",
                     null);
        return cursor;
    }

    public String getTextViewItem(){

        Cursor cursor=null;

        String str="fffff";
        //store query results in cursor
        cursor=myDataBase.rawQuery("SELECT description FROM product_details" ,
                     null);
        Log.i(TAG,"################in gettextview, value of cursor ="+cursor);
        cursor.moveToNext();

        //cast cursor content from the index of 'description Column as a string
        str =cursor.getString(cursor.getColumnIndex("description"));

        return str;
    }

    public void copyDataBase() throws IOException{
        // open db as input stream
        InputStream myInput;
        //open empty db as output stream
        OutputStream myOutPut;
        try {
            myInput = myContext.getAssets().open(DB_NAME);

            //path to newly created db
            String outFileName =DB_PATH + DB_NAME;

            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);
            }
            myOutPut.flush();
            myOutPut.close();
            myInput.close();
            }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        String myPath = DB_PATH + DB_NAME;

        try {
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLException e) {

            e.printStackTrace();
            return false;
        }

        if (checkDB != null){
            checkDB.close();
        }
        return true;
        //return checkDB !=null ? true : false;
    }

    public void openDataBase()throws SQLException{
        //open the database
        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();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...