android解析json,删除并将值插入数据库

时间:2015-04-27 04:32:37

标签: java android json sqlite

我试图通过调用我在数据库类中创建的delete和insert方法来更新值。然而,正如我预期的那样,这是最好的工作。每当调用delete方法的活动将被执行并且给出空值时,并且当第二次再次调用该活动时,它将具有该值(我使用toast文本来获取值并用try和catch包围它将给出异常无效索引#(数组列表索引为零,直到最后一个索引)大小为零)。但是,我正在使用asynctask执行此任务,这是我尝试做的:

@Override
        protected Void doInBackground(Void... arg0) {
            databaseMaster2=new DatabaseMaster2(MainActivity.this);            
            databaseMaster2.getWritableDatabase();
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            jsonStr = jsonStr.replace(
                    "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
            jsonStr = jsonStr.replace("<string>", "");
            jsonStr = jsonStr.replace("</string>", "");
            // Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    // Getting JSON Array node
                    Erates = jsonObj.getJSONArray(TAG_Erate);
                    Arates = jsonObj.getJSONArray(TAG_Arate);
                    Kondisis = jsonObj.getJSONArray(TAG_kondisi);
                    Zonas = jsonObj.getJSONArray(TAG_Zona);
                    tipeASs = jsonObj.getJSONArray(TAG_tipeAs);
                    // looping through All rates
                    for (int i = 0; i < Erates.length(); i++) {
                        JSONObject c = Erates.getJSONObject(i);
                        int id = c.getInt(TAG_ER_ID);
                        int tenor = c.getInt(TAG_ER_TENOR);
                        String ko = c.getString(TAG_ER_K);
                        String co = c.getString(TAG_ER_C);
                        double rate = c.getDouble(TAG_ER_RATE);
                        double ratedb = c.getDouble(TAG_ER_RATE_DB);
                        databaseMaster2.checkErates(id, tenor, ko, co, rate, ratedb);
                        if(databaseMaster2.checkErates(id, tenor, ko, co, rate, ratedb) == true){
                            databaseMaster2.deleteEntryEffectiveRate(id);                           
                            //databaseMaster2.saveEntryEffectiveRate(id, tenor,ko,co,
                                //   rate,ratedb);
                        }
                        else {
                       databaseMaster2.saveEntryEffectiveRate(id, tenor,ko,co,rate,ratedb);
                            //databaseMaster2.deleteEntryAsuransiRate(id);
                        }                   
                    }
                    for (int i = 0; i < Arates.length(); i++) {
                        JSONObject c = Arates.getJSONObject(i);
                        int id = c.getInt(TAG_AS_ID);
                        int zona = c.getInt(TAG_AS_Z);
                        int tenor = c.getInt(TAG_AS_T);
                        double tlo = c.getDouble(TAG_AS_TLO);
                        double comp = c.getDouble(TAG_AS_Comp);
                        double comb = c.getDouble(TAG_AS_comb);
                        // Log.d("Response test: ", id + "\n" + zona + "\n"
                        // + tenor + "\n" + tlo + "\n" + comp + "\n"
                        // + comb);
                        databaseMaster2.checkArates(id, zona, tenor, tlo, comp, comb);
                        if(databaseMaster2.checkArates(id, zona, tenor, tlo, comp, comb) == true){
                            databaseMaster2.deleteEntryAsuransiRate(id);                            
                        }
                        else {
                       databaseMaster2.saveEntryAsuransiRate(id, zona, tenor, tlo, comp, comb);                 
                        }   
                    }
                    for (int i = 0; i < Kondisis.length(); i++) {
                        JSONObject c = Kondisis.getJSONObject(i);
                        int id = 0;
                        String ko = c.getString(TAG_K);
                        String co = c.getString(TAG_C);
                        // Log.d("Response test1: ", ko + "\n" + co);
                        databaseMaster2.checkKondisi(ko, co);
                        if(databaseMaster2.checkKondisi(ko,co) == true){
                            databaseMaster2.deleteKondisi(id);                          
                        }
                        else {
                       databaseMaster2.saveKondisi(ko, co);                         
                        }                                               
                    }
                    for (int i = 0; i < Zonas.length(); i++) {
                        JSONObject c = Zonas.getJSONObject(i);
                        int id = 0;
                        String Za = c.getString(TAG_Za);
                        String Ze = c.getString(TAG_Ze);
                        // Log.d("Response test1: ", Za + "\n" + Ze);
                        databaseMaster2.checkZona(Za, Ze);
                        if(databaseMaster2.checkZona(Za, Ze) == true){
                            databaseMaster2.deleteZona(id);                         
                        }
                        else {
                       databaseMaster2.saveZona(Za, Ze);                    
                        }                                       
                    }
                    for (int i = 0; i < tipeASs.length(); i++) {
                        JSONObject c = tipeASs.getJSONObject(i);
                        int id = 0;
                        String Ty = c.getString(TAG_Ty);
                        // Log.d("Response test1: ", Ty);
                        databaseMaster2.checkType(Ty);
                        if(databaseMaster2.checkType(Ty) == true){
                            databaseMaster2.deleteType(id); 
                        }
                        else {
                       databaseMaster2.saveTipeAsuransi(Ty);

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return null;
        }

我在数据库类中使用的方法(名为DatabaseMaster2): 例如:

public void deleteEntryEffectiveRate(long er_id) throws SQLException{
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_EFFECTIVE_RATE, KEY_ER_ID + " = ?",
                new String[] { String.valueOf(er_id) });
    }
    public void saveEntryEffectiveRate(int id, int tenor,String ko,String co, double rate,
            double ratedb) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues valuesER = new ContentValues();
        valuesER.put(KEY_ER_ID, id);
        valuesER.put(KEY_ER_TENOR, tenor);
        valuesER.put(KEY_ER_KO, ko);
        valuesER.put(KEY_ER_CO, co);
        valuesER.put(KEY_ER_RATE, rate);
        valuesER.put(KEY_ER_RATE_DB, ratedb);
        db.insert(TABLE_EFFECTIVE_RATE, null, valuesER);
    }

并在此处调用:

for (int i = 0; i < Erates.length(); i++) {
JSONObject c = Erates.getJSONObject(i);
    int id = c.getInt(TAG_ER_ID);
    int tenor = c.getInt(TAG_ER_TENOR);
    String ko = c.getString(TAG_ER_K);
    String co = c.getString(TAG_ER_C);
    double rate = c.getDouble(TAG_ER_RATE);
    double ratedb = c.getDouble(TAG_ER_RATE_DB);
    databaseMaster2.checkErates(id, tenor, ko, co, rate, ratedb);
        if(databaseMaster2.checkErates(id, tenor, ko, co, rate, ratedb) == true){
            databaseMaster2.deleteEntryEffectiveRate(id);
            }
            else {
               databaseMaster2.saveEntryEffectiveRate(id, tenor,ko,co,rate,ratedb);
                        }   
                    }

然后检查行是否为空:

public boolean checkErates(int id, int tenor,String ko,String co, double rate,
            double ratedb) 
    {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_EFFECTIVE_RATE, 
                new String[] { KEY_ER_ID,KEY_ER_KO,KEY_ER_CO,KEY_ER_RATE,KEY_ER_RATE_DB }, 
                KEY_ER_ID + " = ? and " + KEY_ER_TENOR + " = ? and "+ KEY_ER_KO + " = ? and " + KEY_ER_CO + " = ? and " + KEY_ER_RATE + " = ? and " + KEY_ER_RATE_DB + " = ?" , 
                new String[] {String.valueOf(id),String.valueOf(tenor),ko,co,String.valueOf(rate),String.valueOf(ratedb)}, 
                null, null, null, null);

        if(cursor.moveToFirst()){

         return true; 
         }//row exists
        else {
         return false;
        }


    }

Logcat for 2nd problem:

04-27 09:57:30.280: E/Database(1018): close() was never explicitly called on database '/data/data/www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom/databases/Rates' 
04-27 09:57:30.280: E/Database(1018): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
04-27 09:57:30.280: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1827)
04-27 09:57:30.280: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
04-27 09:57:30.280: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
04-27 09:57:30.280: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
04-27 09:57:30.280: E/Database(1018):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:541)
04-27 09:57:30.280: E/Database(1018):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
04-27 09:57:30.280: E/Database(1018):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
04-27 09:57:30.280: E/Database(1018):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:530)
04-27 09:57:30.280: E/Database(1018):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:1)
04-27 09:57:30.280: E/Database(1018):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-27 09:57:30.280: E/Database(1018):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-27 09:57:30.280: E/Database(1018):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-27 09:57:30.280: E/Database(1018):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-27 09:57:30.280: E/Database(1018):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-27 09:57:30.280: E/Database(1018):   at java.lang.Thread.run(Thread.java:1019)
04-27 09:57:30.310: E/Database(1018): close() was never explicitly called on database '/data/data/www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom/databases/Rates' 
04-27 09:57:30.310: E/Database(1018): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
04-27 09:57:30.310: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1827)
04-27 09:57:30.310: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
04-27 09:57:30.310: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
04-27 09:57:30.310: E/Database(1018):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
04-27 09:57:30.310: E/Database(1018):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:541)
04-27 09:57:30.310: E/Database(1018):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
04-27 09:57:30.310: E/Database(1018):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
04-27 09:57:30.310: E/Database(1018):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:530)
04-27 09:57:30.310: E/Database(1018):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:1)
04-27 09:57:30.310: E/Database(1018):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-27 09:57:30.310: E/Database(1018):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-27 09:57:30.310: E/Database(1018):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-27 09:57:30.310: E/Database(1018):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-27 09:57:30.310: E/Database(1018):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-27 09:57:30.310: E/Database(1018):   at java.lang.Thread.run(Thread.java:1019)

然后我放DatabaseMaster2.closeDB(); 封闭的方法:

public void closeDB() {
        SQLiteDatabase db = this.getReadableDatabase();
        if (db != null && db.isOpen())
            db.close();
    }

logcat发生了变化:

04-27 11:07:51.170: D/dalvikvm(1604): GC_EXTERNAL_ALLOC freed 68K, 52% free 2624K/5379K, external 1625K/2137K, paused 64ms
    04-27 11:07:56.670: D/dalvikvm(1604): GC_EXTERNAL_ALLOC freed 6K, 51% free 2641K/5379K, external 10738K/10738K, paused 43ms
    04-27 11:08:00.120: W/dalvikvm(1604): threadid=9: thread exiting with uncaught exception (group=0x40015560)
    04-27 11:08:00.140: E/AndroidRuntime(1604): FATAL EXCEPTION: AsyncTask #1
    04-27 11:08:00.140: E/AndroidRuntime(1604): java.lang.RuntimeException: An error occured while executing doInBackground()
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.lang.Thread.run(Thread.java:1019)
    04-27 11:08:00.140: E/AndroidRuntime(1604): Caused by: java.lang.NullPointerException
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.DatabaseMaster2.close(DatabaseMaster2.java:473)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:591)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:1)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    04-27 11:08:00.140: E/AndroidRuntime(1604):     ... 4 more
    04-27 11:08:03.281: E/WindowManager(1604): Activity www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4050a8e8 that was originally added here
    04-27 11:08:03.281: E/WindowManager(1604): android.view.WindowLeaked: Activity www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4050a8e8 that was originally added here
    04-27 11:08:03.281: E/WindowManager(1604):  at android.view.ViewRoot.<init>(ViewRoot.java:258)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.Dialog.show(Dialog.java:241)
    04-27 11:08:03.281: E/WindowManager(1604):  at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.onPreExecute(MainActivity.java:447)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.os.AsyncTask.execute(AsyncTask.java:391)
    04-27 11:08:03.281: E/WindowManager(1604):  at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity.onCreate(MainActivity.java:201)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.os.Handler.dispatchMessage(Handler.java:99)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.os.Looper.loop(Looper.java:123)
    04-27 11:08:03.281: E/WindowManager(1604):  at android.app.ActivityThread.main(ActivityThread.java:3647)
    04-27 11:08:03.281: E/WindowManager(1604):  at java.lang.reflect.Method.invokeNative(Native Method)
    04-27 11:08:03.281: E/WindowManager(1604):  at java.lang.reflect.Method.invoke(Method.java:507)
    04-27 11:08:03.281: E/WindowManager(1604):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    04-27 11:08:03.281: E/WindowManager(1604):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    04-27 11:08:03.281: E/WindowManager(1604):  at dalvik.system.NativeStart.main(Native Method)
    04-27 11:08:06.130: I/Process(1604): Sending signal. PID: 1604 SIG: 9

我把closeDB();放在这里:(在一个for循环然后在所有for循环上,logcat是相同的)

for (int i = 0; i < Erates.length(); i++) {
JSONObject c = Erates.getJSONObject(i);
    int id = c.getInt(TAG_ER_ID);
    int tenor = c.getInt(TAG_ER_TENOR);
    String ko = c.getString(TAG_ER_K);
    String co = c.getString(TAG_ER_C);
    double rate = c.getDouble(TAG_ER_RATE);
    double ratedb = c.getDouble(TAG_ER_RATE_DB);
    databaseMaster2.checkErates(id, tenor, ko, co, rate, ratedb);
        if(databaseMaster2.checkErates(id, tenor, ko, co, rate, ratedb) == true){
            databaseMaster2.deleteEntryEffectiveRate(id);
            }
            else {
                       databaseMaster2.saveEntryEffectiveRate(id, tenor,ko,co,rate,ratedb);
                        }
                        databaseMaster2.closeDB();
                    }

问题:

  1. 当调用它的活动将删除行中的值时,然后当它第二次调用时它将插入值。有什么建议/想法吗?
  2. 在我运行并重新运行应用程序几次后,它会给内存泄漏,说数据库永远不会关闭。在哪里正确放置closeDB();Close();
  3. 注意:解析JSON工作得很好。如果你问我确定,答案肯定是肯定的。证明here

    UPDATE问题1:删除和插入也工作得很好,我想要做的是删除如果行是空的然后插入,否则它将插入(如果它已经是空的)。但它碰巧删除了第一个活动,然后调用给出空值,当我关闭活动并再次调用它时,它将运行将插入数据的else条件。问题是当用户运行应用程序两次时,如果用户只运行1次没有值,那么值就会存在。

    更新问题2:尝试根据答案执行此操作:

    for (int i = 0; i < Erates.length(); i++) {
                            JSONObject c = Erates.getJSONObject(i);
                            int id = c.getInt(TAG_ER_ID);
                            int tenor = c.getInt(TAG_ER_TENOR);
                            String ko = c.getString(TAG_ER_K);
                            String co = c.getString(TAG_ER_C);
                            double rate = c.getDouble(TAG_ER_RATE);
                            double ratedb = c.getDouble(TAG_ER_RATE_DB);
                            boolean boolCheckErates = databaseMaster2.checkErates(
                                    id, tenor, ko, co, rate, ratedb);
                            // close connection here
                            databaseMaster2.closeDB();
                            if (boolCheckErates) {
                                databaseMaster2.deleteEntryEffectiveRate(id);
                                // close connection here
                                databaseMaster2.closeDB();
                            } else {
                                databaseMaster2.saveEntryEffectiveRate(id, tenor,
                                        ko, co, rate, ratedb);
                                // close connection here
                                databaseMaster2.closeDB();
                            }
                        }
    

    在多次重新运行应用程序后,它会出错:

    04-27 10:57:14.000: E/Database(1451): close() was never explicitly called on database '/data/data/www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom/databases/Rates' 
    04-27 10:57:14.000: E/Database(1451): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
    04-27 10:57:14.000: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1827)
    04-27 10:57:14.000: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
    04-27 10:57:14.000: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
    04-27 10:57:14.000: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
    04-27 10:57:14.000: E/Database(1451):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:541)
    04-27 10:57:14.000: E/Database(1451):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    04-27 10:57:14.000: E/Database(1451):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
    04-27 10:57:14.000: E/Database(1451):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:531)
    04-27 10:57:14.000: E/Database(1451):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:1)
    04-27 10:57:14.000: E/Database(1451):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
    04-27 10:57:14.000: E/Database(1451):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    04-27 10:57:14.000: E/Database(1451):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    04-27 10:57:14.000: E/Database(1451):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    04-27 10:57:14.000: E/Database(1451):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    04-27 10:57:14.000: E/Database(1451):   at java.lang.Thread.run(Thread.java:1019)
    04-27 10:57:14.040: E/Database(1451): close() was never explicitly called on database '/data/data/www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom/databases/Rates' 
    04-27 10:57:14.040: E/Database(1451): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
    04-27 10:57:14.040: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1827)
    04-27 10:57:14.040: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
    04-27 10:57:14.040: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
    04-27 10:57:14.040: E/Database(1451):   at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
    04-27 10:57:14.040: E/Database(1451):   at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:541)
    04-27 10:57:14.040: E/Database(1451):   at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    04-27 10:57:14.040: E/Database(1451):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
    04-27 10:57:14.040: E/Database(1451):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:531)
    04-27 10:57:14.040: E/Database(1451):   at www.mandiritunasfinancemobile.co.id.a1s2d3f4g5h6j7k8l9.p0.comcom.MainActivity$GetDatas.doInBackground(MainActivity.java:1)
    04-27 10:57:14.040: E/Database(1451):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
    04-27 10:57:14.040: E/Database(1451):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    04-27 10:57:14.040: E/Database(1451):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    04-27 10:57:14.040: E/Database(1451):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    04-27 10:57:14.040: E/Database(1451):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    04-27 10:57:14.040: E/Database(1451):   at java.lang.Thread.run(Thread.java:1019)
    

    问题1:我想在删除之前插入else条件,是否可能?

    更新问题1 我试试这个并且失败了

    for (int i = 0; i < Erates.length(); i++) {
                            JSONObject c = Erates.getJSONObject(i);
                            int id = c.getInt(TAG_ER_ID);
                            int tenor = c.getInt(TAG_ER_TENOR);
                            String ko = c.getString(TAG_ER_K);
                            String co = c.getString(TAG_ER_C);
                            double rate = c.getDouble(TAG_ER_RATE);
                            double ratedb = c.getDouble(TAG_ER_RATE_DB);
                            boolean boolCheckErates = databaseMaster2.checkErates(
                                    id, tenor, ko, co, rate, ratedb);
                            // close connection here
                            databaseMaster2.closeDB();
                            if (boolCheckErates) {
                                databaseMaster2.deleteEntryEffectiveRate(id);
                                // close connection here
                                databaseMaster2.closeDB();
    
                                databaseMaster2.saveEntryEffectiveRate(id, tenor,
                                        ko, co, rate, ratedb);
                                databaseMaster2.closeDB();
                            } else {
                                databaseMaster2.saveEntryEffectiveRate(id, tenor,
                                        ko, co, rate, ratedb);
                                // close connection here
                                databaseMaster2.closeDB();
                            }
                        }
    

    然后这个:

    for (int i = 0; i < Erates.length(); i++) {
                            JSONObject c = Erates.getJSONObject(i);
                            int id = c.getInt(TAG_ER_ID);
                            int tenor = c.getInt(TAG_ER_TENOR);
                            String ko = c.getString(TAG_ER_K);
                            String co = c.getString(TAG_ER_C);
                            double rate = c.getDouble(TAG_ER_RATE);
                            double ratedb = c.getDouble(TAG_ER_RATE_DB);
                            boolean boolCheckErates = databaseMaster2.checkErates(
                                    id, tenor, ko, co, rate, ratedb);
                            // close connection here
                            databaseMaster2.closeDB();
                            if (boolCheckErates) {
                                databaseMaster2.deleteEntryEffectiveRate(id);
                                // close connection here
                                databaseMaster2.closeDB();
    
                                DatabaseMaster2 databaseMaster3 = new DatabaseMaster2(MainActivity.this);
                                databaseMaster3.getWritableDatabase();
    
                                databaseMaster3.saveEntryEffectiveRate(id, tenor,
                                        ko, co, rate, ratedb);
                                databaseMaster3.closeDB();
                            } else {
                                databaseMaster2.saveEntryEffectiveRate(id, tenor,
                                        ko, co, rate, ratedb);
                                // close connection here
                                databaseMaster2.closeDB();
                            }
                        }
    

    第一次运行顺利然后经过几次(2-7次)重新运行后仍然会出现相同的错误,说从未明确调用close。知道为什么会发生这种情况吗?我只想避免在真实设备上安装应用程序时应用程序崩溃

1 个答案:

答案 0 :(得分:2)

需要正确关闭连接,因为执行新查询。当前实现正试图执行多个查询。

目前多次调用checkErates方法,这不是必需的:

  boolean boolCheckErates=databaseMaster2.checkErates(id, tenor,
                                               ko, co, rate, ratedb);
   // close connection here
      databaseMaster2.closeDB();
  if(boolCheckErates){
        databaseMaster2.deleteEntryEffectiveRate(id);
        // close connection here
        databaseMaster2.closeDB();
   }else{
        databaseMaster2.saveEntryEffectiveRate(id, tenor,ko,co,rate,ratedb);
        // close connection here
        databaseMaster2.closeDB();
   }