无法从Android设备中的资产文件夹复制数据库

时间:2014-04-29 20:19:57

标签: android database sqlite assets

在我的代码中,一切似乎都没问题但是我无法从我的android设备中的资产文件夹中获取数据库。保存在我的资产文件夹中的文件名是 Database.sqlite 请检查我的代码,并向我建议解决方案或指出我在哪里做错了。

DatabaseHandler.java

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 = "Database.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

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


public DatabaseHandler(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);

    this.myContext = context;
    DB_PATH="/data/data/" + context.getPackageName() + "/" + "databases/";
    Log.v("log_tag", "DBPath: " + DB_PATH);
}

/**
 * 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/Database.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();
}

}

MainActity.java

中调用它
    DatabaseHandler myDbHelper = new DatabaseHandler(this);

    boolean dbexist = myDbHelper.checkDatabase();
    if(dbexist)
    {
        System.out.println("Database exists");
        Toast.makeText(getApplicationContext(), "Database exists", Toast.LENGTH_LONG).show();
        myDbHelper.openDatabase(); 
    }
    else
    {
        System.out.println("Database doesn't exist");
        Toast.makeText(getApplicationContext(), "Database doesn't exists", Toast.LENGTH_LONG).show();
        try {
            myDbHelper.createDatabase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

0 个答案:

没有答案