0

I am developing an app (that uses SQLite) in Android Studio 2.3 on Mac OS and would like to view the SQLite database, but can't seem to find the sqlite file. Where are the AVM files and specifically the sqlite file?

I found the AVD folder /Users/<my name>/.android/avd/Pixel_XL_API_26.avd but can't find the user generated data. Any suggestions?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    I believe that AS's emulator doesn't allow you to see the database file (data/data/packagename/databases). However, [Genymotion](https://www.genymotion.com/desktop/) (*an alternative emulator*) does. – MikeT Oct 04 '17 at 02:18
  • The BEST solution was @Bob's answer at https://stackoverflow.com/questions/19194576/how-do-i-view-the-sqlite-database-on-an-android-device – Jeffrey Jul 01 '18 at 18:56

1 Answers1

1

You could always include methods to find out information about the database.

For example you can query the sqlite_master table e.g. :-

    Cursor csr = db.query("sqlite_master",null,null,null,null,null,null);

which equates to :-

    SELECT * FROM sqlite_master

The are PRAGMA statments that allow you to interrogate/change internals PRAGMA Statements.

Note use rawQuery to obtain information into a cursor but use execSQL to make changes e.g. to set and get user_version (Database Version) respectively:-

    db.execSQL("PRAGMA user_version=" + Integer.toString(version));
    Cursor csr = db.rawQuery("PRAGMA user_version",null);

You could also look at the data, output to the log, in the tables with something based upon (where tablename would be changed to the appropriate tablename) :-

    Cursor csr = db.query("tablename",null,null,null,null,null,null);
    String logdata;
    while (csr.moveToNext()) {
        Log.d("DBINFO","Row " + csr.getPosition());
        logdata = "";
        for (int i =0; i < csr.getColumnCount(); i++) {
            String val;
            try {
                val = csr.getString(i);
            }
            catch (Exception e) {
                val = "unobtainable";
            }
            logdata = logdata + "\t\nColumn Name=" +
                    csr.getColumnName(i) +
                    "   Value=" + val;
        }
        Log.d("DBINFO",logdata);
    }
    csr.close();

Note! this only uses getString so will not properly represent some values e.g. double's and floats, if a BLOB then the value will be unobtainable.

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Thanks, an indirect approach such as this works well. – K Owen - Reinstate Monica Oct 06 '17 at 15:58
  • 1
    @koenbro, you might be interested in [Are there any methods that assist with resolving common SQLite issues?](https://stackoverflow.com/questions/46642269/are-there-any-methods-that-assist-with-resolving-common-sqlite-issues), which was posed/answered partly with this question in mind (a more comprehensive permutation of the above) – MikeT Oct 09 '17 at 08:55