0

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!

admdrew
  • 3,790
  • 4
  • 27
  • 39
sdaasd
  • 13
  • 6
  • are you trying to connect two SQLite databases together and make queries on them? not sure if SQLite supports that... if it doesn' you have to import one database into the other somehow – Dave Sep 16 '14 at 17:47

2 Answers2

1

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
}
Chiradeep
  • 973
  • 12
  • 32
1

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"
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • I've created my database with SQLiteOpenHelper, How should I cast SQLiteDatabase to it? – sdaasd Sep 16 '14 at 18:10
  • You can use `getWritableDatabase()` or `getReadableDatabase()` in order to get a `SQLiteDatabase` instance (check out docs for more: http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html); p.s.: see this as well, if you work in a multi-threaded environment: http://stackoverflow.com/questions/2493331/what-are-the-best-practices-for-sqlite-on-android – Trinimon Sep 16 '14 at 18:15
  • Sorry , But Result of SQLiteDatabase.openOrCreateDatabase(file, null); is SQLiteDatabase, My personal database extends SQLiteOpenHelper. I need to use a Object of SQLiteOpenHelper. But here I should convert object of SQLiteDatabase to object of SQLiteOpenHelper. Now Is there way? – sdaasd Sep 16 '14 at 18:24
  • I'd recommend to use a `SQLiteOpenHelper` and to use the statement above in the constructor. Here you find a rather complete example: http://codeblow.com/questions/sqliteopenhelper-creating-database-on-sdcard/; or have a look at this one: http://kiefermat.com/2011/10/07/sqliteopenhelper-with-database-on-sd-card/ – Trinimon Sep 16 '14 at 18:35