0

Super beginner here.

I've created a SQLite database and I want to view it with the SQLiteManager plugin, but my database doesn't add the .db extension on my (virtual) device. So far I've had to copy the file to my computer, change the extension, and push it back in order for the SQLiteManager icon to enable. Then I can see the database.

How can I avoid this run-around?

Also, someone mentioned I shouldn't rely on "/data/data/" to be the correct path. They suggested I use Context.openOrCreateDatabase or one of the get*Path or get*Dir methods. Could someone elaborate on these? The code I used is from a Youtube tutorial I saw.

Here's the code that defines the path:

try {
        String destPath = "/data/data/" + getPackageName() + "/databases/GradesDB.db";
        File f = new File(destPath);
        if(!f.exists()) {
            CopyDB( getBaseContext().getAssets().open("mydb"),
                new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
NSouth
  • 5,067
  • 7
  • 48
  • 83
  • are you extending SQLiteOpenHelper to create your database? if so, you should be supplying a database name in your constructor, just append ".db" on the end of that name. – panini Mar 31 '14 at 22:28
  • **"The code I used is from a Youtube tutorial I saw."** : Ouch! One thing to remember - *anybody* can make a video and post it to YouTube - it doesn't mean they have the slightest idea about what they're doing. That code is wrong in several ways. Never hard-code a file-system path (manufacturers of Android devices can choose to use a different directory structure). Also using `getBaseContext()` is something you'd rarely do unless you have a specific requirement (and know what the implications are). – Squonk Mar 31 '14 at 22:52
  • Thanks, @panini! It was that simple to get the database to be accessible. And @Squonk, I'm trying to figure out why the tutorial included this block of code anyway. What exactly does it do and what's a better way of doing it? – NSouth Apr 02 '14 at 02:19

4 Answers4

3

You should not rely on "data/data" because it may change or it may not work on some devices, getDatabasePath (String name) will always return the correct path, the documentation says

Returns the absolute path on the filesystem where a database created with openOrCreateDatabase

Jorge Alfaro
  • 924
  • 8
  • 11
  • Hey, I've done some reading on DB design and have improved my code's implementation. However, the one part I haven't fixed is my original post. I guess I'm confused as to what it actually does and what I should do instead. Is it creating the database if it doesn't already exist? I had a crash once after this code would have run on a fresh VM and the database file was never created. – NSouth Apr 02 '14 at 02:01
1

this is not the right way to work on data base on android use this guide to create a dbhelper

guide

and after this you can make the db file name whatever you like

l1nuxuser
  • 315
  • 4
  • 11
  • you should avoid just linking to off-site resources, as they can be susceptible link rot. Its better to pull out the relevant information and add that in your answer. – panini Apr 03 '14 at 01:13
1

I know it's late

First at all use this dependency

    compile 'com.ajts.androidmads.sqliteimpex:library:1.0.0'

and use this code to export the database

public static boolean exportDb(Context context, String databaseName) {

        File dbDir = new File(Environment.getExternalStorageDirectory(),"your_path");
        if (!dbDir.exists())
        {
            dbDir.mkdirs();
        }

        SQLiteImporterExporter sqLiteImporterExporter = new SQLiteImporterExporter(context, databaseName);
        try {
            sqLiteImporterExporter.exportDataBase(Environment.getExternalStorageDirectory()+"your_path");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }
Farido mastr
  • 464
  • 5
  • 12
0

I agree with I1nuxuser. Here is another reference: https://github.com/jgilfelt/android-sqlite-asset-helper

Axe
  • 65
  • 8
  • you should avoid just linking to off-site resources, as they can be susceptible link rot. Its better to pull out the relevant information and add that in your answer. – panini Apr 03 '14 at 01:13