我正在创建一个数据库并删除它,在我的android htc mobile sqlite数据库上添加注册表没有任何问题。
但是现在,每次我想创建一个新的数据库后最后删除数据库我得到一个sqlite异常,说我“无法打开数据库文件”,并带有以下数据:
serialVersionUID(RuntimeException)= - 7034897190745766939
cause = SQLiteException(id = 830058513528)
cause = SQLiteException(id = 830058513528)
detailMessage =“无法打开数据库文件”(id = 830058513560) stackState =(id = 830058513672)
stackTrace = null
这是我创建数据库的代码:
private static String DB_PATH = "/data/data/Android.Applications/databases/testdatabase.db";
public void OpenDataBase(){
SQLiteDatabase ApplicationDB = null;
//Si existe la base de datos
if(checkDataBase())
{
//Open readonlymode
String myPath = DB_PATH;
//open database as readwrite mode
ApplicationDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
else
{
try
{
String myPath = DB_PATH ;
ApplicationDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
CreateTables();
//create all the necessary tables
System.out.println("DataBASE CREATED");
}
catch(SQLiteException e)
{
System.out.println("SQL LiteException"+ e.getMessage());
}
}
}
private boolean checkDataBase(){
try{
String myPath = DB_PATH;
ApplicationDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(ApplicationDB != null){
ApplicationDB.close();
}
return ApplicationDB != null ? true : false;
}
这是删除数据库的代码
public boolean DeleteDatabase()
{
boolean wasdeleted;
try
{
File file=new File(DB_PATH);
wasdeleted=file.delete();
}
catch(SecurityException e)
{
wasdeleted=false;
}
return wasdeleted;
}
你可以帮个忙吗?我被困在这里,导致这段代码几个小时前工作......
提前致谢。 最好的祝福。 何。
答案 0 :(得分:2)
您可以考虑切换到使用SQLiteOpenHelper
,这将消除您的大部分代码。