Search code examples
androidmacossqlitexoom

Viewing SQLite data on an Android tablet - SQLite Database Browser


I successfully inserted some data into my sqlite database(I confirmed this by printing out the long ID that is returned from an insert and it was something other than -1) so i know that it is there. problem is is that the sqlite database that I am viewing using the SQLite Database Browser is in the assets folder(i used this to copy already made up data for other tables into the tablet directory) - in this case it is /data/data/packagename/files/ -

Tablet is motorola xoom ICS(4.0.3) and developing on a macbook pro. Any suggestions on what i could use to view the data?

the adb program is in android-sdk-mac_x86/platform-tools and i perform the following command:

./adb shell

  • but i get no where - nothing but permission issues. I saw where someone suggested the firefox add-on but ...really? is that the best there is? Seems like to me there would be a better avenue for developers to look at their data in the database. If you are going to insert stuff into the database you want to be able to look at the contents in case you need to debug your selects later(as a very good example).

EDIT: i cannot get the firefox plugin to work either. Doesnt seem to recognize any external devices.

any ideas? thanks.


Solution

  • I had a similar issue - and decided to put a button in my app that when clicked, copied the database to the SD Card. You can then use DDMS to copy the database from the SD Card to your connected machine and fire up whatever you want to inspect the db, verify values, etc.

    package com.me.myPackage;
    
    import java.io.File;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.content.Context;
    import android.os.Environment;
    import android.widget.Toast;
    
    public class Utility {
    
    public static void backupDatabase(Context context) throws IOException {
    
        // Open your local db as the input stream
        String inFileName = PATH_TO_YOUR_DB;
        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);
    
        File outputDirectory = new File(
                Environment.getExternalStorageDirectory() + "/PATH_FOR_BACKUP");
        outputDirectory.mkdirs();
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_TIME_FORMAT_FOR_DATABASE_NAME); // append date time to db name
    
        String backupFileName = "/MyApp_" + sdf.format(new Date()) + ".db3"; 
        String outFileName = outputDirectory + backupFileName;
    
        // Open the empty db as the output stream
        OutputStream output = new FileOutputStream(outFileName);
    
        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        // Close the streams
        output.flush();
        output.close();
        fis.close();
    
        Toast.makeText(context, "Database backup complete", Toast.LENGTH_LONG)
                .show();
    }
    

    }