K Owen
K Owen

Reputation: 1250

View/edit SQLite database on Android emulator

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?

Upvotes: 0

Views: 643

Answers (1)

MikeT
MikeT

Reputation: 57033

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.

Upvotes: 1

Related Questions