从真实设备和模拟器上的资产文件夹中复制数据库时出错

时间:2014-04-30 11:21:01

标签: android database sqlite

我的应用程序崩溃并在logcat中获取此异常。但是当我在DDMS中检查我的数据库文件夹时,我看到文件中的数据已复制但未在我的列表视图中显示。问题在于createDatabase()函数并从那里获得异常。请参阅下面的logcat堆栈跟踪和我的代码。请帮助我严重困难,不知道该怎么做。

04-30 16:04:32.382: E/AndroidRuntime(20086): FATAL EXCEPTION: main

04-30 16:04:32.382: E/AndroidRuntime(20086): java.lang.Error: Error copying database

04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.WaqasAsgharBhalli.za_traders.DatabaseHandler.createDatabase(DatabaseHandler.java:142)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.WaqasAsgharBhalli.za_traders.DatabaseHandler.<init>(DatabaseHandler.java:121)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.WaqasAsgharBhalli.za_traders.MainActivity.onCreate(MainActivity.java:27)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.Activity.performCreate(Activity.java:4465)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.access$600(ActivityThread.java:122)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)

04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.os.Looper.loop(Looper.java:137)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at android.app.ActivityThread.main(ActivityThread.java:4340)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at java.lang.reflect.Method.invoke(Method.java:511)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 16:04:32.382: E/AndroidRuntime(20086):    at dalvik.system.NativeStart.main(Native Method)

DatabaseHandler.class

 public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables

private static String TAG = DatabaseHandler.class.getName();

// Database Path
 private static String DB_PATH = "/data/data/com.WaqasAsgharBhalli.za_traders/databases/";
  private static String DB_NAME = "myDatabase.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

// Database Version
private static final int DATABASE_VERSION = 1;


public DatabaseHandler (Context context) throws IOException  {
    super(context,DB_NAME,null,1);
    this.myContext=context;
    boolean dbexist = checkDatabase();
    if(dbexist)
    {
        //System.out.println("Database exists");
        openDatabase(); 
    }
    else
    {
        System.out.println("Database doesn't exist");
    createDatabase();
    }

}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */

public void createDatabase() throws IOException{
    boolean dbexist = checkDatabase();
    if(dbexist)
    {
        //System.out.println(" Database exists.");
    }
    else{
        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
 */
public boolean checkDatabase() {
    //SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try{
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
        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 input stream
    InputStream myinput = 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("/data/data/com.WaqasAsgharBhalli.za_traders/databases/myDatabase.sqlite");

    // transfer byte to inputfile to 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;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);

}

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

MainActivity.class

    DatabaseHandler myDbHelper = null;
    try {
        myDbHelper = new DatabaseHandler(this);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        throw new Error("Unable to create database");
    }

1 个答案:

答案 0 :(得分:1)

我建议您使用库在Android中复制数据库。对于不同的Android版本,不同的默认路径位置有很多陷阱。我在我的项目中使用this library来复制数据库,它完全正常。