java - Simultaneous read/write from SQLIte database using Cordova and Android -



java - Simultaneous read/write from SQLIte database using Cordova and Android -

i have application written 90% of cordova(3.5.0) , 10% of java. 10% written in java used synchronize database external api. synchronization occurs in next way:

@override protected void onpreexecute() { this.checkversionforinvalidoptionsmenu(); } @override protected boolean doinbackground(void... params) { log.d("execute", "task"); homecoming sync(); } protected void onpostexecute(boolean result) { this.checkversionforinvalidoptionsmenu(); if (result) { toast.maketext(this.ac.getapplicationcontext(), r.string.curr_sync, toast.length_short).show(); this.notificationprogress.setcontenttext(this.ac.getstring(r.string.notification_complete)) .setprogress(0,0,false) .setautocancel(true); this.notificationmanage.notify(0, notificationprogress.build()); } closedb(); } public boolean sync() { if (!syncinitialdata()) homecoming false; homecoming true; } public boolean syncinitialdata() { string syncdata = sfzapi.getinitialdata(); if(syncdata != null) { seek { sqlqueries.execsql(db, syncdata, this); } grab (sqlexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (illegalargumentexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming true; } homecoming false; }

i utilize asynctask. here code , method: sqlqueries.execsql (db, syncdata, this);

public static void execsql(sqlitedatabase db, string filename, syncdatabase task) throws sqlexception, ioexception { // todo: count lines int alllines = 26000; bufferedreader br = new bufferedreader(new filereader(filename)); map<string, object> map = new hashmap<string, object>(); map.put("filename", filename); map.put("task", task); map.put("alllines", alllines); afterthirteenapiversion(br, db, map); br.close(); removefile(filename); } private static void afterthirteenapiversion(bufferedreader br, sqlitedatabase db, map<string, object> map) throws sqlexception, ioexception { string line; int counter = 0; int lastpercent = 0; db.begintransaction(); while ((line = br.readline()) != null) { if(line.trim().length() == 0) continue; sqlitestatement stmt = db.compilestatement(line); stmt.execute(); stmt.clearbindings(); stmt.close(); int percent = (int) ((counter * 100) / ((integer)map.get("alllines"))); if (percent != lastpercent) { ((syncdatabase)map.get("task")).doprogress(percent); lastpercent = percent; } counter++; } db.settransactionsuccessful(); db.endtransaction(); }

this class makes whole transaction records sql came external api , executes transaction.

while sync want @ same time able search in app. have field used search. here sample code implements part of functionality behind field:

define([ 'underscore', 'backbone', 'sqlinstall', 'models/schedule/schedulemodel' ], function(_, backbone, sqlinstall, schedulemodel){ var schedulescollection = backbone.collection.extend({ model: schedulemodel, initialize: function() { this.schedulemodel = new schedulemodel(); this.db = getdatabase(); }, fetch: function(line, typetransport, callback, direction, stopuid) { var self = this; this.reset(); this.db.transaction(function(tx) { self.schedulemodel.gettransport(tx, line, typetransport, self, callback, direction, stopuid); }, this.errordb, this.successdb); }, errordb: function(err) { console.log(err); if(err.code == 5) { new schedulescollection().notexistrecord(); return; } console.log(err); }, successdb: function() { }, homecoming schedulescollection; }); gettransport: function(tx, line, typetransport, collection, callback, direction, stopuid) { var self = this; var typetransport = getcorrecttype(typetransport); var sql = this.sqldefaultquery(line, typetransport); tx.executesql(sql, [], function(tx, result) { (var i=0; < result.rows.length; i++){ collection.add({ transport: result.rows.item(i).num, typetransport: converttohtmltypetransport(result.rows.item(i).type_transport_short), direction: result.rows.item(i).from_to_stop, stopname: result.rows.item(i).name, stopuid: result.rows.item(i).uid_stop, stoplat: result.rows.item(i).latitude, stoplon: result.rows.item(i).longitude, schedule: $.parsejson(result.rows.item(i).time), }); } self.adddirections(tx, line, typetransport, collection, callback, direction); callback(); }); },

when reach code executed in error , fails. transaction executed problems database , somehow deletes itself.

i tried create transactions on part, runs somewhat, , in synchronization states base of operations closed , 1 time again receives error base of operations closed.

if wait synchronization finish , seek no problems. problem occurs when seek simultaneously write , read. since java write , read cordova (ie js).

someone faced similar problem. utilize sqlite java , web sqlite cordova.

if need more code details give.

java android database sqlite cordova

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -