引起:java.lang.IllegalStateException:无法执行此操作,因为Android中的连接池已关闭

时间:2016-05-19 06:46:42

标签: java android sqlite

我正在尝试从sqlite数据库获取列表视图中的所有记录,但是获得 IllegalStateException的异常:无法执行此操作,因为连接池已经关闭。一次又一次。如果我删除 db.close(); ,即使遇到同样的问题。如何解决这个问题。

这是我在db helper类中的代码。

public class MyDbHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "ClassNKK.db";
    public static final int DATABASE_VERSION = 4;
    private Context context;
    public SQLiteDatabase db;
    String str_Authentication_Token;
    String str_LoginUserId;
    String str_UserName;
    String str_UserRole;


    public MyDbHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        // TODO Auto-generated constructor stub
        this.context = context;

 @Override
    public void onCreate(SQLiteDatabase db) {.....}


@Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS " + Table_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Table_COMPANY);
        db.execSQL("DROP TABLE IF EXISTS " + Table_VesselList);
        db.execSQL("DROP TABLE IF EXISTS " + Table_Inspector_List);
        db.execSQL("DROP TABLE IF EXISTS " + Table_AllPost_Table);
        db.execSQL("DROP TABLE IF EXISTS " + Table_ActivityObjectList);
        db.execSQL("DROP TABLE IF EXISTS " + Table_FollowUpList);
        db.execSQL("DROP TABLE IF EXISTS " + Table_SyncTime);
        db.execSQL("DROP TABLE IF EXISTS " + Table_VEsselHashTag);
        db.execSQL("DROP TABLE IF EXISTS " + Table_FollowUpSettings);

        onCreate(db);


    public boolean update_AllPost(String strActivityStatus, String strActivityId,
                                  String strRemark,
                                  String strName,
                                  String strInspectorName,
                                  String strShortname,
                                  String strHashTag,
                                  String strStatus,
                                  String strType,
                                  String strUserId,
                                  String strObjectId,

                                  String strActiondate,
                                  String strStarFlag,
                                  String strDownLoadStatus,
                                  String strUpLoadStatus,
                                  String strStarMarkUpLoadStatus,
                                  String strUpLoad_FollowUpStatus,
                                  String strUpLoad_UnFollowUpStatus )
    {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_AllPost_ActivityStatus, strActivityStatus);
        args.put(COL_AllPost_PostActivityId, strActivityId);
        args.put(COL_AllPost_Remark, strRemark);
        args.put(COL_AllPost_Title, strName);
        args.put(COL_AllPost_inspectorname, strInspectorName);
        args.put(COL_AllPost_shortName, strShortname);
        args.put(COL_AllPost_HashTag, strHashTag);
        args.put(COL_AllPost_Status, strStatus);
        args.put(COL_AllPost_Type, strType);

        args.put(COL_AllPost_UserId, strUserId);
        args.put(COL_AllPost_VesselId, strObjectId);

        args.put(COL_AllPost_ActionDate, strActiondate);
        args.put(COL_AllPost_StarFlag, strStarFlag);
        args.put(COL_AllPost_DownLoadStatus, strDownLoadStatus);
        args.put(COL_ALLPOST_UpLoad_Status , strUpLoadStatus);
        args.put(COL_ALLPOST_UpLoad_StarMark_Status ,strStarMarkUpLoadStatus);
        args.put(COL_ALLPOST_UpLoad_FollowUpStatus ,strUpLoad_FollowUpStatus);
        args.put(COL_ALLPOST_UpLoad_UnFollowUpStatus ,strUpLoad_UnFollowUpStatus);

        int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "=" + strActivityId, null);

        return i > 0;

    }




    public boolean update_FollowUpPost(String Followup,
                                       String FollowupType,
                                       String activityId,
                                       String createdBy,
                                       String date,
                                       String editableFlag,
                                       String follow_id,
                                       int followupId,
                                       String objectId,
                                       String followUp_UpLoadStatus
                                      )
    {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_Followup, Followup);
        args.put(COL_FollowupType , FollowupType);
        args.put(COL_Followup_activityId, activityId);
        args.put(COL_Followup_createdBy, createdBy);
        args.put(COL_Followup_date, date);
        args.put(COL_Followup_editableFlag, editableFlag);
        args.put(COL_FID, follow_id);
        args.put(COL_followupId, followupId);
        args.put(COL_Followup_objectId, objectId);
        args.put(COL_FollowUp_UpLoadStatus , followUp_UpLoadStatus);

        int i = db1.update(Table_FollowUpList, args, COL_followupId + "=" + followupId, null);

        return i > 0;

    }

    }

 public boolean isAllPostId_Exist(String activityId) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_AllPost_Table, new String[]{COL_AllPost_PostActivityId}, COL_AllPost_PostActivityId + "=?",

                new String[]{String.valueOf(activityId)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                cursor.close();
                db.close();
                return false;

            } else {
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }

    public boolean isConpany_IdExit(String strCompanyId) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_COMPANY, new String[]{COL_Company_Id}, COL_Company_Id + "=?",

                new String[]{String.valueOf(strCompanyId)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                //   Log.e("Row is  Not  ", " Exist in Table_VesselList Table in MyDbHelper");

                cursor.close();
                db.close();
                return false;

            } else {

                //  Log.e("Row is   already ", " Exist in Table_VesselList Table in MyDbHelper ");
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }

    public boolean isActivityObject_ImageAudioPath(String str_ImageAudioPath) {

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(Table_ActivityObjectList, new String[]{COL_imageaudioPath}, COL_imageaudioPath + "=?",

                new String[]{String.valueOf(str_ImageAudioPath)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {

                cursor.close();
                db.close();
                return false;

            } else {


                cursor.close();
                 db.close();
                return true;
            }
        }
        return false;
    }

    public boolean isExistNotation(String str_Notation) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_VEsselHashTag, new String[]{VesselHashTag}, VesselHashTag + "=?",

                new String[]{String.valueOf(str_Notation)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                //Log.e("Row is Not  ", " Exist in ActivityObjectList Table in MyDbHelper");
                cursor.close();
                db.close();
                return false;

            } else {

                //Log.e("Row is already ", " Exist in ActivityObjectList Table in MyDbHelper ");
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }

 public void insertCompany(String strCompanyId, String strCompanyName) {
        SQLiteDatabase db1 = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_Company_Id, strCompanyId);
        values.put(COL_CompanyName, strCompanyName);
        db1.insert(Table_COMPANY, null, values);
        db1.close();
    }

    /*********************************************************************************************************/
    @Override
    public synchronized void close() {
        if (db != null) {
            db.close();
            super.close();
        }
    }

    /********************************************************************************************************/

    public boolean updateStarFlag(String str_ActivityId, String str_StarFlag, String str_StarMark_UpLoadStatus) {
        SQLiteDatabase db1 = getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put(COL_AllPost_PostActivityId, str_ActivityId);
        args.put(COL_AllPost_StarFlag, str_StarFlag);
        args.put(COL_ALLPOST_UpLoad_StarMark_Status, str_StarMark_UpLoadStatus);

        int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "= ? ", new String[]{str_ActivityId});

        return i > 0;
    }

    public void insert_VesselList(String strACtiveStatus, String strVessCompanyId, String strVesselName, String strVesseleId, String strVesselsTypeId, String VesselsTypeName, String strClassCodeNumber, String strIsFollowedVessels) {

        SQLiteDatabase db = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL_ACtiveStatus, strACtiveStatus);
        values.put(COL_VessCompanyId, strVessCompanyId);
        values.put(COL_VesselName, strVesselName);
        values.put(COL_VesseleId, strVesseleId);
        values.put(COL_VesselsTypeId, strVesselsTypeId);
        values.put(COL_VesselsTypeName, VesselsTypeName);
        values.put(COL_ClassCodeNumber, strClassCodeNumber);
        values.put(COL_IsFollowedVessels, strIsFollowedVessels);

        db.insert(Table_VesselList, null, values);
        db.close();
    }

    public boolean isUserDetails_Exist(String strUserName) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(Table_NAME, new String[]{COL_Username}, COL_Username + "=?",

                new String[]{String.valueOf(strUserName)}, null, null, null, null);

        if (cursor != null) {

            if (cursor.getCount() == 0) {
                // Log.e("Row is  Not  ", " Exist in Table_VesselList Table in MyDbHelper");

                cursor.close();
                db.close();
                return false;

            } else {

                //  Log.e("Row is   already ", " Exist in Table_VesselList Table in MyDbHelper ");
                cursor.close();
                db.close();
                return true;
            }
        }
        return false;
    }
 public List<All_Post> getMyAll_StarPost(int OFFSET) {
    List<All_Post> allPostDescList = new ArrayList<All_Post>();
    String selectQuery = "select * from  " + Table_AllPost_Table + " where StarFlag='1' AND ActiveStatus = '1' AND Status = 'PENDING' ORDER BY ActionDate DESC LIMIT 50 OFFSET "+OFFSET ; // LIMIT 50 OFFSET "+OFFSET;

    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            All_Post allPostDesc = new All_Post();
            allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
            allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
            allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
            allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
            allPostDesc.setStrProfilePic(cursor.getString(cursor.getColumnIndex("ProfilePICURL")));
            allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
            allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
            allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
            allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));
            String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
            allPostDesc.setStrStarFlag(cursor.getString(cursor.getColumnIndex("StarFlag")));
            allPostDesc.setStrDownLoadStatus(cursor.getString(cursor.getColumnIndex("DownLoad_Status")));

            if (strSqliteDate != null && !strSqliteDate.isEmpty()) {
                String result = "";
                Date date1 = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date1 = dateFormat.parse(strSqliteDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd");
                result = printFormat.format(calendar.getTime());
                //Log.e("result = ","==========>"+result);
                allPostDesc.setActiondate(result);
            }


            allPostDescList.add(allPostDesc);

        } while (cursor.moveToNext());
        db.close();

    }
    return allPostDescList;
}

public List<All_Post> getMyAllPost(String strUserId , int OFFSET) {
    List<All_Post> allPostDescList = new ArrayList<All_Post>();
    String selectQuery = "select * from  " + Table_AllPost_Table + " where  UserId=" + strUserId + " AND ActiveStatus = '1' AND Status = 'PENDING' ORDER BY ActionDate DESC LIMIT 50 OFFSET "+OFFSET;

    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            All_Post allPostDesc = new All_Post();
            allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
            allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
            allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
            allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
            allPostDesc.setStrProfilePic(cursor.getString(cursor.getColumnIndex("ProfilePICURL")));
            allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
            allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
            allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
            allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));
            String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
            allPostDesc.setStrStarFlag(cursor.getString(cursor.getColumnIndex("StarFlag")));
            allPostDesc.setStrDownLoadStatus(cursor.getString(cursor.getColumnIndex("DownLoad_Status")));


            if (strSqliteDate != null && !strSqliteDate.isEmpty()) {
                String result = "";
                Date date1 = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date1 = dateFormat.parse(strSqliteDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd");
                result = printFormat.format(calendar.getTime());
                //Log.e("result = ","==========>"+result);
                allPostDesc.setActiondate(result);
            }

            allPostDescList.add(allPostDesc);

        } while (cursor.moveToNext());
        db.close();

    }
    return allPostDescList;
}

public List<All_Post> getAllDescriptions(int OFFSET)
{
    List<All_Post> allPostDescList = new ArrayList<All_Post>();
    String selectQuery = "select * from  " + Table_AllPost_Table + " where  ActiveStatus = '1' AND Status = 'PENDING' ORDER BY ActionDate DESC LIMIT 50 OFFSET "+OFFSET; //ORDER BY ActionDate DESC
    db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            All_Post allPostDesc = new All_Post();
            allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
            allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
            allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
            allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
            allPostDesc.setStrProfilePic(cursor.getString(cursor.getColumnIndex("ProfilePICURL")));
            allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
            allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
            allPostDesc.setStrShortName(cursor.getString(cursor.getColumnIndex("Type")));
            allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
            allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));

            String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));

            if (strSqliteDate != null && !strSqliteDate.isEmpty()) {
                String result = "";
                Date date1 = null;
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date1 = dateFormat.parse(strSqliteDate);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                SimpleDateFormat printFormat = new SimpleDateFormat("dd-MM-yyyy");
                result = printFormat.format(calendar.getTime());
                //Log.e("result = ","==========>"+result);
                allPostDesc.setActiondate(result);
            }

            allPostDesc.setStrStarFlag(cursor.getString(cursor.getColumnIndex("StarFlag")));
            allPostDesc.setStrDownLoadStatus(cursor.getString(cursor.getColumnIndex("DownLoad_Status")));
            allPostDescList.add(allPostDesc);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return allPostDescList;
}

    }

0 个答案:

没有答案