2012-11-23 57 views
1

執行我收到異常日誌作爲「錯誤代碼5:DATABSE被鎖定」,而從asynktask

11-23 11:25:56.110: E/Database(543): Error inserting PartyCode=PM001 PaymentMode=0 Amount=12.2 ReceiptDate=1353522600000 SalesmanCode=SL1 
11-23 11:25:56.110: E/Database(543): android.database.sqlite.SQLiteException: error code 5: database is locked 
11-23 11:25:56.110: E/Database(543): at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 
11-23 11:25:56.110: E/Database(543): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 
11-23 11:25:56.110: E/Database(543): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 
11-23 11:25:56.110: E/Database(543): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410) 
11-23 11:25:56.110: E/Database(543): at com.virtuosoitech.dsr.dataaccess.ReceiptDataManager.insertNewReceipt(ReceiptDataManager.java:244) 
11-23 11:25:56.110: E/Database(543): at com.virtuosoitech.dsr.ui.GlobalSalesReportOpt.onDestroy(GlobalSalesReportOpt.java:1460) 
11-23 11:25:56.110: E/Database(543): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642) 
11-23 11:25:56.110: E/Database(543): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673) 
11-23 11:25:56.110: E/Database(543): at android.app.ActivityThread.access$2900(ActivityThread.java:125) 
11-23 11:25:56.110: E/Database(543): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
11-23 11:25:56.110: E/Database(543): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-23 11:25:56.110: E/Database(543): at android.os.Looper.loop(Looper.java:123) 
11-23 11:25:56.110: E/Database(543): at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-23 11:25:56.110: E/Database(543): at java.lang.reflect.Method.invokeNative(Native Method) 
11-23 11:25:56.110: E/Database(543): at java.lang.reflect.Method.invoke(Method.java:521) 
11-23 11:25:56.110: E/Database(543): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-23 11:25:56.110: E/Database(543): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-23 11:25:56.110: E/Database(543): at dalvik.system.NativeStart.main(Native Method) 

而取消asynktask的執行,並試圖插入到數據庫中的新條目

class HttpTask extends AsyncTask<String, Void, SalesDetails> { 



     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 



      spinningDialog = new ProgressDialog(GlobalSalesReportOpt.this); 
      spinningDialog.setMessage("Please wait.."); 
      spinningDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      spinningDialog.setCancelable(true); 
      spinningDialog.show(); 
     } 


     @Override 
     protected SalesDetails doInBackground(
       String... params) { 
      // TODO Auto-generated method stub 


      ArrayList<InvoiceBrand> invoice_data_brandList = (ArrayList<InvoiceBrand>) SalesDataManager 
        .getInstance().getOptimizedSalesData(
          getApplicationContext(), group, party, item, 
          company, brand, family, datefrom, dateTo); 

} 



     @Override 
     protected void onPostExecute(SalesDetails details) { 
      // TODO Auto-generated method stub 

      Log.e("GlobalSalesReport", "onCancelled()"); 
      uiStartTime = new Date().getTime(); 
      if (spinningDialog == null) { 

       return; 
      } 

      spinningDialog.dismiss(); 
      spinningDialog = null; 

} 

和插入我的樣本DB類是

public long insertNewReceipt(Context context, Receipt receipt) { 

    SQLiteOpenHelper helper = null; 
    SQLiteDatabase db = null; 
    long orderId = 0; 

    try { 


     helper = new DSRDBHelper(context); 
     db = helper.getWritableDatabase(); 


     ContentValues receiptValues = new ContentValues(); 

     receiptValues.put("ReceiptDate", Util.getDate(receipt.getReceiptDate()).getTime()); 

     if (receipt.getPaymentMode() == Constants.CASH_TYPE){ 


      receiptValues.put("PaymentMode", Constants.CASH_TYPE); 

     } 
     else if (receipt.getPaymentMode() == Constants.CHEQUE_TYPE){ 

      receiptValues.put("PaymentMode", Constants.CHEQUE_TYPE); 
      receiptValues.put("ChqueDate", receipt.getChequeDate()); 
      receiptValues.put("ChqueNo", receipt.getChequeNo()); 
      receiptValues.put("BankName", receipt.getBankName()); 

     } 

     receiptValues.put("Amount", receipt.getAmount()); 
     receiptValues.put("PartyCode", receipt.getPartyCode()); 
     receiptValues.put("SalesmanCode", receipt.getSalesmanCode()); 

     orderId = db.insert("Receipt", null, receiptValues); 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

    } finally { 
     if (helper != null) { 
      helper.close(); 
     } 
     if (db != null) { 
      db.close(); 
     } 
    } 

    return orderId; 
} 

所以請幫助我解決這個問題..我曾嘗試使用單身模式的數據庫幫助,但它不能幫助我...而且是我的數據庫類違反一般做法(我應該使用內容提供者,而不是??)..如果是在向我提供的鏈接就..

+0

在插入打開您的數據庫之前。 – rajeshwaran

+0

它已經打開.. – NullPointerException

+0

請參閱此鏈接http://stackoverflow.com/questions/7657223/sqlite-exception-database-is-locked-issue,http://stackoverflow.com/questions/8360400/error-code -5-database-is-locked-when-using-a-contentprovider – rajeshwaran

回答

1

放在一個單獨的類(Singleton類)數據庫連接

和執行數據庫操作有 例:

public class MyDaoImpl { 

    private Context _context = null; 
    private SQLiteDatabase _myDB = null; 
    private static MyDaoImpl _myDaoImpl = null; 


    public static synchronized AdminUserDaoImpl getInstance(Context context) { 
     if (_myDaoImpl == null) { 
      _myDaoImpl = new AdminUserDaoImpl(context); 
     } 
     return _myDaoImpl; 
    } 

    private AdminUserDaoImpl(Context context) { 
     super(); 
     this._context = context; 
    } 

    /* write your db operation methods here*/ 



     /** 
    * This method will check if the database is opened or not and close Database 
    */ 
    public void closeDBConnection(){ 

     if(_myDB != null){ 
      if(_myDB.isOpen()){ 
       _myDB.close(); 
      } 
     } 
    } 
} 
在異步或活動課

使用像

MyDaoImpl myDaoImpl = MyDaoImpl.getInstance(getBaseContext()); 
myDaoImpl.doOperation1(); 
myDaoImpl.closeDBConnection(); 
+0

請讓我知道,這是否對你有用。 – kumar