0
我有一個服務,它將在服務的幫助下每隔10分鐘用數據更新Sqllite數據庫。我已打開數據庫連接,並在數據更新時關閉連接。運行服務時發生數據庫鎖定錯誤
在我的應用程序中,我有不同的數據庫更新發生的頁面。我也打開了數據庫連接,並關閉了每個頁面的連接以進行數據更新。
問題是,雖然服務正在運行,但我無法在服務中打開連接時通過我的應用程序將數據更新到sqlite。
有什麼方法可以使用服務和應用程序來同時運行數據庫更新。
任何人都可以幫助樣品。
以下(代碼)
public class service_helper extends Service {
public static final String TAG = "Service";
private NotificationManager mNM;
private DatabaseAdapter dbAdapter;
private service_updator serviceUpdator;
private int NOTIFICATION = 1;
private Timer timer = new Timer();
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
serviceUpdator = new service_updator(this);
dbAdapter = new DatabaseAdapter(this);
dbAdapter.open();
Toast.makeText(this, "Service created ...", Toast.LENGTH_LONG).show();
startService() ;
}
@Override
public void onStart(Intent intent, int startid) {
}
@Override
public void onDestroy() {
timer.cancel();
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
private void startService()
{
timer.scheduleAtFixedRate(new mainTask(), 0, 250000);
}
private class mainTask extends TimerTask
{
public void run()
{
try {
serviceUpdator.UploadData();
Log.d(TAG, "Service");
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private Runnable threadBody = new Runnable() {
public void run() {
try {
serviceUpdator.UploadData();
Log.d(TAG, "Service");
} catch (ParserConfigurationException e) {
}
}
};
}
UploadData() Function Code
dbAdapter = new DatabaseAdapter(this.context);
dbAdapter.open();
Long transactionType = null;
String transactionData = null;
String sql = "Select * from tblTransaction where PKTransaction >?";
Cursor cursorTransaction = dbAdapter.ExecuteRawQuery(sql, "-1");
cursorTransaction.moveToFirst();
dbAdapter.close();
for (int i = 0; i < cursorTransaction.getCount(); i++) {
}
dbAdapter.close();
Application Code
private void SaveSortOrder() throws Exception {
try {
String server1IPAddress = "";
String server2IPAddress = "";
String deviceId = "";
Cursor cursorTransaction;
Cursor cursorAdmin;
DatabaseAdapter dbAdapter;
DataXmlExporter dataXmlExporter;
admin_helper adminhelper;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
String RevisedEstimatedDate = sdf.format(date);
adminhelper = new admin_helper(this);
cursorAdmin = adminhelper.GetAdminDetails();
if (cursorAdmin.moveToFirst())
server1IPAddress = cursorAdmin.getString(cursorAdmin
.getColumnIndex("RemoteServer1IPAddress"));
server2IPAddress = cursorAdmin.getString(cursorAdmin
.getColumnIndex("RemoteServer2IPAddress"));
deviceId = cursorAdmin.getString(cursorAdmin
.getColumnIndex("DeviceID"));
cursorAdmin.close();
ContentValues initialSortOrder = new ContentValues();
ContentValues initialTransaction = new ContentValues();
for (int i = 0; i < ListSortOrder.getAdapter().getCount(); i++) {
HashMap result = (HashMap) ListSortOrder.getItemAtPosition(i);
View vListSortOrder;
vListSortOrder = ListSortOrder.getChildAt(i);
TextView Sort_DeliveryOrder = (TextView) vListSortOrder
.findViewById(R.id.et_Sort_Order);
initialSortOrder.put("DeliveryOrder", Sort_DeliveryOrder
.getText().toString());
dbAdapter = new DatabaseAdapter(this);
dbAdapter.open();
dbAdapter.BeginTransaction();
dbAdapter.UpdateRecord("tblDelivery", initialSortOrder,
"PKDelivery" + "="
+ result.get("Sort_PKDelivery").toString(),
null);
dataXmlExporter = new DataXmlExporter(this);
dataXmlExporter.StartDataSet();
String sqlTransaction = "Select 5 as TransactionType,'Update Delivery Order' as Description,'"
+ result.get("Sort_PKDelivery").toString()
+ "' as FKDelivery, "
+ " deviceId as DeviceID ,'"
+ Sort_DeliveryOrder.getText().toString()
+ "' as DeliveryOrder ,date() as TransactionUploadDate,time() as TransactionUploadTime from tblAdmin where PKAdmin > ?";
cursorTransaction = dbAdapter.ExecuteRawQuery(sqlTransaction,
"-1");
dataXmlExporter.AddRowandColumns(cursorTransaction,
"Transaction");
String XMLTransactionData = dataXmlExporter.EndDataSet();
try {
if ((server1IPAddress != "") && (server2IPAddress != "")) {
try {
if (server1IPAddress != "") {
InsertUploadedTrancasctionDetails(
server1IPAddress, deviceId,
XMLTransactionData);
}
} catch (Exception exception) {
if ((server1IPAddress != server2IPAddress)
&& (server2IPAddress != "")) {
InsertUploadedTrancasctionDetails(
server2IPAddress, deviceId,
XMLTransactionData);
}
}
}
} catch (Exception exception) {
initialTransaction
.put("ReceivedDate", RevisedEstimatedDate);
initialTransaction.put("TransactionData",
XMLTransactionData);
dbAdapter.InsertRecord("tblTransaction", "",
initialTransaction);
}
dbAdapter.SetSucessfulTransaction();
dbAdapter.EndTransaction();
dbAdapter.close();
}
} catch (Exception exception) {
throw exception;
}
}
我有一個類似的應用程序誰服務每5分鐘運行一次,我沒有問題。你是SQLiteHelper類應該處理各種請求沒有問題。你能爲此發佈代碼嗎? – SBerg413
我已經放置了代碼 –