我是Android开发的新手,所以我有一个名为Courses的现有数据库和DatabaseHelper文件,它将从Android工作室的assets文件夹中复制现有文件,但它运行循环并创建一个带有android_manifest文件的空数据库, "没有表格被复制"。因此,给我和运行选择表时找不到表格的错误。
错误: E / AndroidRuntime:致命异常:主要 进程:com.example.sinhanikita17.myapplication,PID:15127 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sinhanikita17.myapplication / com.example.sinhanikita17.myapplication.Choose}:android.database.sqlite。 SQLiteException:没有这样的表:ICS(代码1):,
公共类DatabaseHelper扩展了SQLiteOpenHelper { private static String DB_PATH;
private static String DB_NAME = "Courses.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static final String LOGTAG = "Myapp5";
private static DatabaseHelper _dbHelper;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
Log.i(LOGTAG, DB_PATH);
}
public static DatabaseHelper getInstance(Context context)
{
if(_dbHelper == null)
{
_dbHelper = new DatabaseHelper(context);
}
return _dbHelper;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database doesn't exist yet.
}
if(checkDB != null) checkDB.close();
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open("Courses.db");
Log.i(LOGTAG, String.valueOf(myContext.getAssets()));
// 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(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();
}