我正在尝试使 dataAdapter 扩展 SQLiteOpenHelper。下面是代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import com.muoiot.enlistening.idefined.*;
import com.muoiot.enlistening.utilities.MsgUtils;
public class DbAdapter extends SQLiteOpenHelper implements DB_INFO, DB_USER, DB_QUESTION {
public static String defaultDbPath = "";
protected SQLiteDatabase databaseControl;
private Context context = null;
public DbAdapter(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
defaultDbPath = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
}
/***********************************************************************************/
// This function auto call at the first time when the database was not found.
/***********************************************************************************/
@Override
public void onCreate(SQLiteDatabase db) {
MsgUtils.DB("Don't have database, need to copy from asset");
try {
copyDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/***********************************************************************************/
// This function auto call when the database has been opened.
/***********************************************************************************/
@Override
public void onOpen(SQLiteDatabase db){
MsgUtils.DB(" AAAAAAAAAAAAAAAAAAAAAAAAA has been opened!!!");
}
/***********************************************************************************/
// Copy database
/***********************************************************************************/
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = this.context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = defaultDbPath + DB_NAME;
// 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();
}
/***********************************************************************************/
// Open and close database
/***********************************************************************************/
public void openDatabase(){
databaseControl = this.getWritableDatabase();
}
public void closeDatabase(){
this.getWritableDatabase().close();
}
}
而且,现在我遇到了 copyDataBase 函数的问题。我总是得到一个没有任何表的数据库。(我的手机已经root了,所以我从data/data//databases复制回数据库来查看)。
我在这里错了什么?任何人都可以帮我一把吗?