mardi 27 octobre 2015

Android SQLite cursor returns 33 results, but debug only outputs 17

Hi folks I've got a strange cade. I'm trying to debug the SQLite DB in an app. If I do a query SELECT * from table I get 33 results, but if I iterate over the cursor it ends at result #17.

Here's my debug class (the method in question is 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 -- at row : " + Integer.toString(row) );
                                        break;
                                }
                                StringBuilder line = new StringBuilder(512);

                                if (colCount <= 0)
                                        pw.println("Empty row?");

                                for(int i = 0; i < 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 at 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 + " - this is not a 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 and 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 FROM sqlite_master WHERE 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();

                for (int i = 0; i < tableNames.size(); i++)
                {
                        String table = tableNames.get(i);
                        c = db.rawQuery("SELECT * FROM " + table +  " LIMIT 100 ", null);
                        WriteToFile(c, "all" , table, table);
                        c.close();
                }
        }
}

The output csv file is:

tables: app - from AppDAO.bulkInsertApp
query: SELECT * FROM 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,http://ift.tt/1LzVz6y, 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,http://ift.tt/1N5FrvG Inc.,NULL,0,,   Varies with 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,http://ift.tt/1LzVz6A Inc.,NULL,0,, 3.1.0  ,News & Magazines,NULL,NULL,1,NULL,NULL,NULL
6,8248,com.google.android.gm,Gmail,http://ift.tt/1N5Ft73 Inc.,NULL,0,,   Varies with devic,Communication,NULL,NULL,1,NULL,NULL,NULL
7,8250,com.google.android.music,Google Play Music,http://ift.tt/1LzVwrn Inc.,NULL,0,,   Varies with devic,Music & Audio,NULL,NULL,1,NULL,NULL,NULL
8,8253,com.google.android.videos,Google Play Movies & TV,http://ift.tt/1N5FrvL Inc.,NULL,0,,   Varies with devic,Media & Video,NULL,NULL,1,NULL,NULL,NULL
9,8312,com.android.chrome,Chrome Browser - Google,http://ift.tt/1LzVwrp Inc.,NULL,0,,   Varies with devic,Communication,NULL,NULL,1,NULL,NULL,NULL
10,8316,com.google.android.calendar,Google Calendar,http://ift.tt/1N5FrvP Inc.,NULL,0,, 201308023  ,Productivity,NULL,NULL,1,NULL,NULL,NULL
11,8433,com.estrongs.android.pop,ES File Explorer  File Manager,http://ift.tt/1LzVz6C APP Group,NULL,0,,   Varies with devic,Productivity,NULL,NULL,1,NULL,NULL,NULL
12,8867,com.devhd.feedly,Feedly,http://ift.tt/1N5Ftnl 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,http://ift.tt/1LzVzmQ 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!

Aucun commentaire:

Enregistrer un commentaire