2010-01-29 47 views
0

我正在創建一個數據庫並刪除它,在我的android htc mobile sqlite數據庫中添加註冊表沒有任何問題。在android中創建數據庫

但現在,數據庫每次的最後刪除後,我想創建一個新的我得到一個SQLite的例外,說我「無法打開數據庫文件」具有下列數據:

的serialVersionUID(RuntimeException的)= -7034897190745766939
原因= SQLiteException(ID = 830058513528)
原因= SQLiteException(ID = 830058513528)
detailMessage = 「無法打開數據庫文件」(ID = 830058513560) stackState =(ID = 830058513672)
stackTrace = n ULL

這是我的代碼來創建數據庫:

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; 
     } 

能給我一隻手的代碼?即時通訊stucked在這裏,導致此代碼工作幾個小時前...

在此先感謝。 最好的問候。 何塞。

回答

2

您可能會考慮切換爲使用SQLiteOpenHelper,這會消除大部分代碼。

+0

嗨, 當前,我展示的代碼是擴展到SQLiteOpenHelper類。 謝謝。 此致敬禮。 何塞。 – Sosi 2010-01-29 16:14:07

+0

SQLiteOpenHelper子類應該看起來完全不像你發佈的東西。例如,您不要在SQLiteOpenHelper中打開自己的數據庫 - SQLiteDatabase對象作爲onCreate()和onUpgrade()的參數提供給您。 – CommonsWare 2010-01-30 01:54:29