我无法将数据库从Asset文件夹复制到数据库文件夹。我使用这段代码:
public class DBVocab extends SQLiteOpenHelper {
private static String DB_PATH= "data/data/APP_NAME/databases/";
private static String DB_NAME = "wocab";
private SQLiteDatabase dbObj;
private Context context;
public DBVocab(Context context) {
super(context, DB_NAME, null, 3);
this. context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDB() throws IOException {
this.getWritableDatabase();
Log.i("Readable ends....................","end");
try {
copyDB();
Log.i("copy db ends....................","end");
} catch (IOException e) {
throw new Error("Error copying database");
}
}
String dialogText = "Copying Database";
private Runnable changeMessage = new Runnable() {
@Override
public void run() {
//Log.v(TAG, strCharacters);
Main.pDialog.setMessage(dialogText);
}
};
private void showLoading(){
}
public boolean checkDB(){
SQLiteDatabase checkDB = null;
ProgressDialog pD = Main.pDialog;
try{
String path = DB_PATH + DB_NAME;
Log.i("myPath ......",path);
checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
Log.i("myPath ......",path);
if (checkDB!=null)
{
CardsBackup cards = new CardsBackup();
Cursor c= checkDB.rawQuery("SELECT * FROM wocab", null);
//Log.i("Cursor.......",c.getString(0));
c.moveToFirst();
String contents[]=new String[100];
int flag=0;
while(! c.isAfterLast())
{
String temp="";
int s1=c.getInt(0);
pD.setProgress(s1);
String word=c.getString(1);
String phonetic=c.getString(2);
String type=c.getString(3);
String mean=c.getString(4);
String sample=c.getString(5);
String sample_mean=c.getString(6);
String grade=c.getString(7);
if(flag<contents.length) contents[flag]=temp;
flag=flag+1;
Log.i("DB values.........",temp);
try{
cards.add(context,word,phonetic,type,mean,sample,sample_mean,grade);
} catch (SQLException e){
e.printStackTrace();
Log.i("Add to Database ......... ","failure to ADD");
Toast.makeText(context, "failure to add!", Toast.LENGTH_SHORT).show();
}
c.moveToNext();
}
c.moveToFirst();
while(! c.isAfterLast()){
String temp="";
int s1=c.getInt(0)+2911;
pD.setProgress(s1);
if(s1 >= 825+2911) break;
String word=c.getString(1);
String phonetic=c.getString(2);
String type=c.getString(3);
String mean=c.getString(4);
String sample=c.getString(5);
String sample_mean=c.getString(6);
String grade="7";
if(flag<contents.length) contents[flag]=temp;
flag=flag+1
try{
cards.add(context,word,phonetic,type,mean,sample,sample_mean,grade);
} catch (SQLException e){
e.printStackTrace();
Toast.makeText(context, "failure to add!", Toast.LENGTH_SHORT).show();
}
c.moveToNext();
}
Log.i("DB count.........", ""+flag);
}
else
{
return false;
}
}catch(SQLiteException e){
e.printStackTrace();
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
public void copyDB() throws IOException{
try {
Log.i("inside copyDB....................","start");
InputStream ip = context.getAssets().open(DB_NAME+".db");
Log.i("Input Stream....",ip+"");
String op= DB_PATH + DB_NAME ;
OutputStream output = new FileOutputStream( op);
byte[] buffer = new byte[1024];
int length;
while ((length = ip.read(buffer))>0){
output.write(buffer, 0, length);
//Log.i("Content.... ",length+"");
}
output.flush();
output.close();
ip.close();
}
catch (IOException e) {
Log.v("error", e.toString());
}
}
public void openDB() throws SQLException {
String myPath = DB_PATH + DB_NAME;
dbObj = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.e("db","open DB......"+dbObj.toString());
}
@Override
public synchronized void close() {
if(dbObj != null)
dbObj.close();
super.close();
}
}
它适用于我的设备。 但在某些Android设备中,它会在复制数据库中间崩溃。 资源文件夹中的数据库有2911个单词,我重新复制了800个第一个单词。
有人可以帮帮我吗? 感谢。