1

我得到的异常日志为

 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)

同时取消异步任务的执行 尝试在数据库中插入新条目

  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;

}

我的插入示例数据库类是

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;
}

所以请帮我解决这个问题..我尝试使用单例模式作为 DB 助手,但它不能帮助我......我的 DB 类是否违反了一般做法(我应该使用内容提供程序吗??).. 如果是为我提供有关..的链接

4

1 回答 1

1

将您的数据库连接放在一个单独的类中(单例类)

并在那里执行数据库操作 例如:

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();
            }
        }
    }
} 

在您的 Async 或 Activity 类中

使用喜欢

MyDaoImpl myDaoImpl = MyDaoImpl.getInstance(getBaseContext());
myDaoImpl.doOperation1();
myDaoImpl.closeDBConnection();
于 2012-11-23T07:25:50.253 回答