sdaasd
sdaasd

Reputation: 13

Connecting database to local folder in android

I have database files in SD Card in android, I want to connect SQLite database directly to SD card path and use them directly.

Is there any way to do this?

If not, should I copy database files into /data/data/package/databases/? Is there a way to do that?

I've spend about 4 hours trying this, but I haven't gotten any results!

Upvotes: 0

Views: 162

Answers (2)

Trinimon
Trinimon

Reputation: 13967

Yes there is, open the database with ...

File file = new File("/sdcard/my.db" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(file, null);

... and don't forget to add the external storage permissions :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Path sdcard above might be replaced by

Environment.getExternalStorageDirectory() +"/" + "my.db"

Upvotes: 1

Chiradeep
Chiradeep

Reputation: 991

Have a look at this article.It should be enough for you.

http://androidexample.com/SQLite_Database_Manipulation_Class_-_Android_Example/index.php?view=article_discription&aid=51

EDIT

Handling SQLite is very easy and methodological procedure.

PROCEDURE 1(This is internal Storage) Check whethere you have already the db in your external path or not.To do that use this below code.Please note this set of code should be written in DBhelper class onCreate method.

if (!new File("/data/data/" + this.getPackageName()
        + "/database.sqlite").exists()) {
    try {
        FileOutputStream out = new FileOutputStream("data/data/"
                + this.getPackageName() + "/database.sqlite");
        InputStream in = getAssets().open("databases/dic.db");

        byte[] buffer = new byte[1024];
        int readBytes = 0;

        while ((readBytes = in.read(buffer)) != -1)
            out.write(buffer, 0, readBytes);

        in.close();
        out.close();
    } catch (IOException e) {
    }
}else{
  /database already exists.Do nothing
}

You should place this code, in your MainActivity's onCreate function. You can easily use your own database in whole of your project. To access your database, you can use the code as follow :

   SQLiteDatabase sqliteDB = SQLiteDatabase.openOrCreateDatabase(
            "/data/data/" + this.getPackageName() + "/database.sqlite",
            null);

   Cursor cursor = sqliteDB.rawQuery("SELECT * FROM table", null); // example of query

PROCEDURE 2(This is for External Storage) Create a COnstructor in DBHelper Class

public DatabaseHelper(final Context context) { super(context, Environment.getExternalStorageDirectory() + File.separator + FILE_DIR + File.separator + DATABASE_NAME, null, DATABASE_VERSION); }

When you will be making the object of this DBHelper class the new db will be created in the external Path.And in OnCreate you check whether that particular path is exists or not like this:

if (!new File(Environment.getExternalStorageDirectory()
        + File.separator + FILE_DIR
        + File.separator + DATABASE_NAME).exists()) {
    SQLiteDatabase.openOrCreateDatabase(Environment.getExternalStorageDirectory()
        + File.separator + FILE_DIR
        + File.separator + DATABASE_NAME,null);
}else{
  /database already exists.Do nothing
}

Upvotes: 1

Related Questions