我将异常日志视为
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;
}
我的示例数据库类用于插入
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 :(得分: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();