java - Android SQLite cursor returns 33 results, but debug only outputs 17 -
hi folks i've got strange cade. i'm trying debug sqlite db in app. if query select * table
33 results, if iterate on cursor ends @ result #17.
here's debug class (the method in question public static void writetofile(cursor cursor, string query , string tables, string uri)
) :
package com.s2u.android.ps; import java.io.file; import java.io.filewriter; import java.io.printwriter; import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.list; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.environment; import android.util.log; import com.s2u.android.ps.bo.app; import com.s2u.android.ps.bo.appmember; import com.s2u.android.ps.datamodel.databasemanager; import com.s2u.android.ps.networkapis.androidlog; import com.s2u.android.ps.networkapis.appconfig; public class debugtofile { private static string tag = "debugtofile"; private static file path = new file(environment.getexternalstoragedirectory() + "/ps_debug"); public static void writetofile(string lines , string tag) { writetofile(lines , tag , "txt"); } public static void writetofile(string lines , string tag , string ext) { if (!validate("writetofile(string lines)")) return; file file = new file(path, tag + "_" + getdatestamptens() + "." + ext); try { filewriter fw = new filewriter(file.getabsolutepath() , true); printwriter pw = new printwriter(fw); pw.println(getdatestamp() + " - " + lines); pw.println(); pw.flush(); pw.close(); //log.e(tag, "writetofile(string lines) - " + file.tostring()); } catch (exception e) { log.e(tag, "writetofile(string lines) failed!", e); } } public static void writetofileappmember(arraylist<appmember> appmembers , string tag) { if (!validate("writetofile(appmember)")) return; file file = new file(path, tag + "_" + getdatestamptens() + ".csv"); try { filewriter fw = new filewriter(file.getabsolutepath() , true); printwriter pw = new printwriter(fw); pw.println(getdatestamp() + " - " + "appmembers"); boolean doonce = true; for(com.s2u.android.ps.bo.appmember appmember : appmembers) { if (doonce) { doonce = false; pw.println(appmember.getcsvlabels()); } pw.println(appmember.getcsvstring()); } pw.println(); pw.flush(); pw.close(); //log.e(tag, "writetofile(string lines) - " + file.tostring()); } catch (exception e) { log.e(tag, "writetofile(string lines) failed!", e); } } public static void writetofileappmember(list<appmember> appmembers , string tag) { if (!validate("writetofile(appmember)")) return; file file = new file(path, tag + "_" + getdatestamptens() + ".csv"); try { filewriter fw = new filewriter(file.getabsolutepath() , true); printwriter pw = new printwriter(fw); pw.println(getdatestamp() + " - " + "appmembers"); boolean doonce = true; for(com.s2u.android.ps.bo.appmember appmember : appmembers) { if (doonce) { doonce = false; pw.println(appmember.getcsvlabels()); } pw.println(appmember.getcsvstring()); } pw.println(); pw.flush(); pw.close(); //log.e(tag, "writetofile(string lines) - " + file.tostring()); } catch (exception e) { log.e(tag, "writetofile(string lines) failed!", e); } } public static void writetofileapps(list<app> apps , string tag) { if (!validate("writetofile(appmember)")) return; file file = new file(path, tag + "_" + getdatestamptens() + ".csv"); try { filewriter fw = new filewriter(file.getabsolutepath() , true); printwriter pw = new printwriter(fw); pw.println(getdatestamp() + " - " + "app objects"); boolean doonce = true; for(com.s2u.android.ps.bo.app app : apps) { if (doonce) { doonce = false; pw.println(app.getcsvlabels()); } pw.println(app.getcsvstring()); } pw.println(); pw.flush(); pw.close(); //log.e(tag, "writetofile(string lines) - " + file.tostring()); } catch (exception e) { log.e(tag, "writetofile(string lines) failed!", e); } } public static void writetofile(cursor cursor, string query , string tables, string uri) { if (!validate("writetofile(cursor)")) return; file file = new file(path, uri + "_" + getdatestamptens() + ".csv"); try { filewriter fw = new filewriter(file.getabsolutepath(), true); printwriter pw = new printwriter(fw); int resultcount = cursor.getcount(); pw.println("time: " + getdatestamp()); pw.println("tables: " + tables); pw.println("query: " + query); pw.println("result count: " + integer.tostring(resultcount)); int row = 0; string labels = "row,"; int startposition = cursor.getposition(); cursor.movetoposition(-1); while (cursor.movetonext()) { int colcount = cursor.getcolumncount(); row++; if (row >= resultcount) { pw.println("error! rows >= cursor count -- @ row : " + integer.tostring(row) ); break; } stringbuilder line = new stringbuilder(512); if (colcount <= 0) pw.println("empty row?"); for(int = 0; < colcount; i++) { if (row == 1) { labels += cursor.getcolumnname(i) + "[" + getcursorfieldtypestring(cursor, i) + "]"; if (i < colcount - 1) labels += ","; } if (i == 0) line.append(integer.tostring(row) + ","); line.append(getcursorstring(cursor, i)); if (i < colcount - 1) { line.append(","); } } if (row == 1) pw.println(labels); pw.println(line.tostring()); cursor.movetonext(); if (row > 100) { pw.println("max rows output - stopped @ row: " + integer.tostring(row)); break; } } pw.println("end"); pw.println(); pw.flush(); pw.close(); //log.e(tag, "writetofile(cursor) - " + file.tostring()); cursor.movetoposition(startposition); } catch (exception e) { log.e(tag, "writetofile(cursor) failed!", e); } } private static boolean validate(string methodname) { if (!appconfig.istestbuild()) { log.i(tag, methodname + " - not test build!"); return false; } if (!isexternalstoragewritable()) { androidlog.e(tag, methodname + " - external storage not accessible"); return false; } if (!path.exists()) { path.mkdir(); if (!path.exists()) { androidlog.e(tag, methodname + " - directory doesn't exist , couldn't create it: " + path.tostring()); return false; } } return true; } private static string getdatestamp() { calendar c = calendar.getinstance(); simpledateformat df = new simpledateformat("yyyymmdd-kk:mm:ss.sss"); string date = df.format(c.gettime()); return date; } private static string getdatestamptens() { string date = getdatestamp(); date = date.substring(0,date.length() - 1) + "0"; return date; } private static boolean isexternalstoragewritable() { string state = environment.getexternalstoragestate(); if (environment.media_mounted.equals(state)) { return true; } return false; } private static string getcursorstring(cursor cursor, integer i) { string result = "undefined"; switch(cursor.gettype(i)) { case cursor.field_type_null: result = "null"; break; case cursor.field_type_blob: result = "blob length: " + integer.tostring(cursor.getblob(i).length); break; case cursor.field_type_float: result = float.tostring(cursor.getfloat(i)); break; case cursor.field_type_integer: result = integer.tostring(cursor.getint(i)); break; case cursor.field_type_string: result = cursor.getstring(i); break; default: result = "undefined cursor value type(" + integer.tostring(cursor.gettype(i)) + ") -- try getstring: " + cursor.getstring(i); } result.replace("", " "); return result; } private static string getcursorfieldtypestring(cursor cursor, integer i) { string result = "unk"; switch(cursor.gettype(i)) { case cursor.field_type_null: result = "null"; break; case cursor.field_type_blob: result = "blob"; break; case cursor.field_type_float: result = "f"; break; case cursor.field_type_integer: result = "int"; break; case cursor.field_type_string: result = "str"; break; default: result = "unk(" + integer.tostring(cursor.gettype(i)) + ") "; } return result; } public static string applisttypetostring(int applisttype) { if (applisttype == 0) return "kapplistmain"; else if (applisttype == 1) return "kapplistprofile"; else if (applisttype == 2) return "kapplistpromoted"; return "unknown list type int: " + integer.tostring(applisttype); } public static void dumpdatabasetofiles(databasemanager db) { sqlitedatabase readabledb = db.getreadabledatabase(); dumpdatabasetofiles(readabledb); } public static void dumpdatabasetofiles(sqlitedatabase db) { if (!validate("dumpdatabasetofiles")) return; cursor c = db.rawquery("select name sqlite_master type='table'", null); if (c.getcount() <= 0) { writetofile("table name count: " + integer.tostring(c.getcount()) , "dbdump_err"); c.close(); return; } //androidlog.i(tag , "dumpdb table count: " + integer.tostring(c.getcount())); list<string> tablenames = new arraylist<string>(); if (c.movetofirst()) { while(!c.isafterlast()) { tablenames.add(c.getstring(c.getcolumnindex("name"))); c.movetonext(); } } c.close(); (int = 0; < tablenames.size(); i++) { string table = tablenames.get(i); c = db.rawquery("select * " + table + " limit 100 ", null); writetofile(c, "all" , table, table); c.close(); } } }
the output csv file is:
tables: app - appdao.bulkinsertapp query: select * app result count: 33 row,_id[int],packagename[str],appname[str],iconurl1[str],iconurl2[null],publisher[str],publisheremail[null],price[int],currency[str],version[str],category[str],releasedate[null],updatedon[null],hastried[int],promo_url[null],promoparam[null],promovaluekey[null] 1,8192,com.shared2you.android.powerslyde,powerslyde,https://lh5.ggpht.com/1qigt9zz7oh5ktfiis9ukjljvtm7w-ur34xzcaqhfjc9glmzatj-atrwyb6gxqhscheu=w300,null,shared2you, inc.,null,0,, 1.08 ,lifestyle,null,null,1,null,null,null 2,8219,com.android.providers.downloads.ui,com.android.providers.downloads.ui,null,null,null,null,null,,null,null,null,null,1,null,null,null 3,8225,com.google.android.apps.maps,maps,https://lh3.ggpht.com/jw-f0fkebhpkyh8ldcyq7cvetrynygbyvbh9huqnjxw4x64orhofjisdowhekulemw0=w300,null,google inc.,null,0,, varies devic,travel & local,null,null,1,null,null,null 4,8231,com.android.vending,com.android.vending,null,null,null,null,null,,null,null,null,null,1,null,null,null 5,8246,com.google.android.apps.magazines,google play newsstand,https://lh5.ggpht.com/rowopaiodov-bng7rnd6awpzwlnoc7vzab-29gpfvb6jfe8dhor42owbqamluxj-w2si=w300,null,google inc.,null,0,, 3.1.0 ,news & magazines,null,null,1,null,null,null 6,8248,com.google.android.gm,gmail,https://lh4.ggpht.com/ebn-cw55bnkwg7ng5nugpijvpjeabta-upijd4kekbhpedz29svdj3ezkfr20zzzzne=w300,null,google inc.,null,0,, varies devic,communication,null,null,1,null,null,null 7,8250,com.google.android.music,google play music,https://lh6.ggpht.com/5opwbg-m6yfcjwzjz1llt05yif2alyiy9ytpqm1f6u42lxwmcvb54m1zekv9-hcaotc=w300,null,google inc.,null,0,, varies devic,music & audio,null,null,1,null,null,null 8,8253,com.google.android.videos,google play movies & tv,https://lh5.ggpht.com/ffpqtalnnu4xflvbazvbwpl5o4x3a_cqyhuwih4fxmfu78assup1omkgxhxouxxzwpov=w300,null,google inc.,null,0,, varies devic,media & video,null,null,1,null,null,null 9,8312,com.android.chrome,chrome browser - google,https://lh6.ggpht.com/lum4kyb0ttgvr-8vrmuz_jhrnmq4yqbir0yjspc4etsm9ij8-4yhz0s0ho9i0ez_=w300,null,google inc.,null,0,, varies devic,communication,null,null,1,null,null,null 10,8316,com.google.android.calendar,google calendar,https://lh5.ggpht.com/qgupybpstb61cpriji9yxv3bey00t5bhobugdpextdesqev9b9-j8_zds_clqzpbskc=w300,null,google inc.,null,0,, 201308023 ,productivity,null,null,1,null,null,null 11,8433,com.estrongs.android.pop,es file explorer file manager,https://lh5.ggpht.com/p31ciabf5umc1wbjxv2spt4tsllqfquzpp8n0ateaa0zemxxv_njvdiswvkjeuuss2w=w300,null,es app group,null,0,, varies devic,productivity,null,null,1,null,null,null 12,8867,com.devhd.feedly,feedly,https://lh4.ggpht.com/rkoudgwbt3wnztdra5qvnn8satdk3zehhwomhzbiu2vlf3-9hllmh89w9gjpgetxo3u=w300,null,feedly team,null,0,, 18.1.2 ,news & magazines,null,null,1,null,null,null 13,8917,com.google.android.email,com.google.android.email,null,null,null,null,null,,null,null,null,null,1,null,null,null 14,12113,com.google.android.play.games,google play games,https://lh5.ggpht.com/tkg8ndu21rjzo5wsz7jrpyj35p-odtm0md2snwvvobtq0ke_orhhorrzqwcjvtevxp8_=w300,null,google inc.,null,0,, 1.1.04 ,entertainment,null,null,1,null,null,null 15,87853,com.google.android.apps.docs.editors.sheets,null,null,null,null,null,null,null,null,null,null,null,1,null,null,null 16,87862,com.google.android.apps.photos,null,null,null,null,null,null,null,null,null,null,null,1,null,null,null 17,87867,com.umfersolutions.eatthiszombies,null,null,null,null,null,null,null,null,null,null,null,1,null,null,null end
thanks!
you advancing cursor position 2 times, 1 in
while (cursor.movetonext())
and other 1 @ end of loop in
pw.println(line.tostring()); cursor.movetonext();
thats why half of results, since @ end move 1 position, , @ when checking while
condition advance again, reading position 0, position 2, 4...and on...
Comments
Post a Comment