我正在尝试在 android 中写入我的 sqlite 数据库中的表,我正在使用的代码如下:
DBH = new DatabaseHelper(this);
try {
DBH.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBH.getWritableDatabase().rawQuery("insert into profile (_id, FirstName) values (1, 'Dave')", null);
DBH.close();
DBH 是我的数据库助手类,它可以很好地创建数据库,我什至可以从数据库中读取数据,所以我知道它在那里。我试图写入的表是配置文件,我确信它确实存在,但每次我运行这个我都会收到这个错误:
sqlite returned: error code = 1, msg = no such table: profile
我已经尝试从这张表中阅读并能够得到结果,所以我非常难过。有人有什么想法吗?
编辑:根据要求,这是我的数据库助手类
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.ProjectZeus.FitnessWire/databases/";
private static String DB_NAME = "fitnesswire.db";
private SQLiteDatabase myDB;
private Context myContext;
public DatabaseHelper(Context context) {
super(context,DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean check = checkDataBase();
if(check){
//Log.e("Check", "Database exists");
}else{
this.getReadableDatabase();
try{
copyDataBase();
}catch(Exception e){
throw new Error("Error copying database");
}
}
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte [] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkdb = null;
try{
String myPath= DB_PATH + DB_NAME;
checkdb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
Log.e("checkDataBase()", "Database doesntexist yet");
}
if(checkdb != null){
checkdb.close();
}
return checkdb != null ? true : false ;
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close(){
if(myDB != null){
myDB.close();
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}