无法调试

时间:2011-12-21 04:44:30

标签: android sqlite

我无法调试。无法找到关闭数据库的位置。

我的db helper class ::

 private static final String DATABASE_NAME = "aviva.db";    
        private static final int DATABASE_VERSION = 1;  
        private static final String TABLE_NAME_MY_DETAILS = "my_details";  
        private static final String TABLE_NAME_ADDITIONAL_INFORMATION = "additional_information";  

      OpenHelper openHelper ;

      private Context context;  
        private SQLiteDatabase db;  

    private SQLiteStatement insertStmtMyDetails,insertStmtAdditionalInfo;
    private static final String INSERT_MY_DETAILS = "insert into " 
      + TABLE_NAME_MY_DETAILS + "(policy_number,policy_exp_date,last_name,first_name,DOB,emergency_contact) values (?,?,?,?,?,?)";
   private static final String INSERT_ADDITIONAL_INFORMATION = "insert into "
          + TABLE_NAME_ADDITIONAL_INFORMATION + "(claimant_location,loss_date,claimant_description,image1,image2) values (?,?,?,?,?)";

      public DataHelper(Context context) {
       this.context = context;
       openHelper = new OpenHelper(this.context);
       this.db = openHelper.getWritableDatabase();
        this.insertStmtMyDetails = this.db.compileStatement(INSERT_MY_DETAILS);
      this.insertStmtAdditionalInfo=this.db.compileStatement(INSERT_ADDITIONAL_INFORMATION); 
        openHelper.close();  
     }

     public void Close(){
       db.close();
       openHelper=null;
     }

      public long insertMyDetails(String policy_number,String policy_exp_date,String last_name,String first_name,String DOB,String emergency_contact) {  
       this.insertStmtMyDetails.bindString(1, policy_number);  
        this.insertStmtMyDetails.bindString(2, policy_exp_date);
        this.insertStmtMyDetails.bindString(3, last_name);
       this.insertStmtMyDetails.bindString(4, first_name);
       this.insertStmtMyDetails.bindString(5, DOB);
       this.insertStmtMyDetails.bindString(6, emergency_contact);   
       System.out.println("data inserted");
       return this.insertStmtMyDetails.executeInsert();
    }  

     public long insertAdditionalInfo(String claimant_location,String loss_date,String claimant_description,String image1,String image2 ){
           this.insertStmtAdditionalInfo.bindString(1, claimant_location);
           this.insertStmtAdditionalInfo.bindString(2, loss_date);
           this.insertStmtAdditionalInfo.bindString(3, claimant_description);
           this.insertStmtAdditionalInfo.bindString(4, image1);
           this.insertStmtAdditionalInfo.bindString(5, image2);
           System.out.println("data inserted");
           return this.insertStmtAdditionalInfo.executeInsert();
        }

     public void deleteAll() {
       this.db.delete(TABLE_NAME_MY_DETAILS, null, null);
       this.db.delete(TABLE_NAME_ADDITIONAL_INFORMATION, null, null);
    }
 private static class OpenHelper extends SQLiteOpenHelper {

      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
          db.execSQL("CREATE TABLE " + TABLE_NAME_MY_DETAILS + " (id INTEGER PRIMARY KEY, policy_number TEXT,policy_exp_date TEXT,last_name TEXT,first_name TEXT,DOB TEXT,emergency_contact TEXT)");
          db.execSQL("CREATE TABLE " + TABLE_NAME_ADDITIONAL_INFORMATION + " (id INTEGER PRIMARY KEY, claimant_location TEXT,loss_date TEXT,claimant_description TEXT,image1 TEXT,image2 TEXT)");
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_MY_DETAILS);
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_ADDITIONAL_INFORMATION);
         onCreate(db);
      }
   }
}     

日志:

 12-21 10:17:29.916: ERROR/Database(893): java.lang.IllegalStateException: mPrograms   size 2
 12-21 10:17:29.916: ERROR/Database(893):     at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1669)  
 12-21 10:17:29.916: ERROR/Database(893):     at dalvik.system.NativeStart.run(Native Method)  
 12-21 10:17:29.916: ERROR/Database(893): Caused by:       java.lang.IllegalStateException: /data/data/com.test.aviva.insurance/databases/aviva.db SQLiteDatabase created and never closed  
 12-21 10:17:29.916: ERROR/Database(893):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)  
 .....
 12-21 10:17:29.985: ERROR/Database(893): Leak found  
 12-21 10:17:29.985: ERROR/Database(893): Caused by:      java.lang.IllegalStateException: /data/data/com.test.aviva.insurance/databases/aviva.db SQLiteDatabase created and never closed  
...  
 12-21 10:17:29.985: ERROR/Database(893):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)

由于

1 个答案:

答案 0 :(得分:1)

在执行查询到数据库之前打开数据库并在执行查询后关闭数据库是一种很好的做法。

另请参阅When to close db connection on android? Every time after your operation finished or after your app exit