2013-02-12 65 views
1

在我的应用程序中,我正在锁定数据库。数据库被锁定getReadableDatabase()android

我尝试使用其他帖子提供的解决方案,但找不到合适的解决方案,这就是为什么我发布这个问题。

这里是的onCreate

DBAdapter db = new DBAdapter(this.getApplicationContext()); 
     dialoge = new Progress_Dialog(this); 
     dialoge.setCancelable(false); 
     try { 
      db.createDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

,这里是创建数据库方法

public void createDataBase() throws IOException{  
      boolean dbExist = checkDataBase();  
      if(!dbExist){ 
       this.getReadableDatabase();  
       try {  
        copyDataBase(); 
       } catch (IOException e) {  
        e.printStackTrace(); 
        throw new Error("Error copying database");  
       } 
      } 
     } 

以下是checkdatabase方法

private boolean checkDataBase(){  
      SQLiteDatabase checkDB = null; 
      try{ 
       String myPath = DB_PATH + DB_NAME; 

       String myPath2 = DB_PATH + DB_NAME2; 

       checkDB = SQLiteDatabase.openDatabase(myPath, null, 
         SQLiteDatabase.OPEN_READWRITE); 

       if(checkDB != null){ 
        checkDB.close(); 

       } 
       checkDB = SQLiteDatabase.openDatabase(myPath2, null, 
         SQLiteDatabase.OPEN_READWRITE); 

      }catch(SQLiteException e){ 
     //  e.printStackTrace(); 
       //database does't exist yet. 
       System.out.print("SQLiteException "+e.toString()); 
      } 
      if(checkDB != null){ 
       checkDB.close(); 

      } 
      return checkDB != null ? true : false; 
     } 

请帮我在这。

回答

6

的“锁定”发生,因为你正试图打开多个SQLiteDatabase实例。按照此post中所述将您的SQLiteDatabase设为单身。

+0

你的解决方案似乎很好,让我使用它,并验证它是否解决了我的问题,但为+1的好信息在后:) – 2013-02-12 05:41:45

-4

更改此方法this.getReadableDatabase(); to this.getWritableDatabase();

它会工作...

+0

请您详细说明该解决方案将如何工作? – 2013-02-12 05:31:28

+0

这应该没有什么区别。 – 2013-02-12 05:32:03

+0

我们创建数据库的目的只有可读或可写。在ur代码中数据库的目的是读写所以它的强制性要调用this.getWritableDatabase();请相应地更改并运行它。你的代码是checkDB = SQLiteDatabase.openDatabase(myPath,null, SQLiteDatabase.OPEN_READWRITE); – sri 2013-02-12 06:04:43