Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store sqlite database directly on sdcard

Tags:

i want to create my sqlite database in sdcard instead of default path...i want to access all my data from sdcard also I have Used This Code:

            private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase.openOrCreateDatabase("/sdcard/"+DATABASE_NAME,null);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "
                + TABLE_NAME
                + " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT, image BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

Problem:

When i see the database file in default path i can see all the data and table but when i see the database file created in sd card it doesnot shows any data but it only shows the database file

IN constructor it only creates the file in sdcard but in default path it does everything well..... How to store all Sqlitedata on sdcard for further access?

like image 386
Shiv Avatar asked Jan 17 '13 07:01

Shiv


People also ask

How are SQLite databases stored?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases. However, there are no restrictions on creating databases elsewhere.

Where should I put SQLite database?

There is no "standard place" for a sqlite database. The file's location is specified to the library, and may be in your home directory, in the invoking program's folder, or any other place. If it helps, sqlite databases are, by convention, named with a . db file extension.

Can SQLite store an entire database in a single file?

Afaik, SQLite stores a single database in a single file.

Can I store SQLite database on Google Drive?

There is also a button which will show the data from google drive and user can edit and update the data so the updated data is then store in the google drive. I'm new in Android development please help me. you can compress your database file in ZIP and send that zip to server. then send that .


1 Answers

I created my DB with

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

and had no problems further on. I guess your call to super() should reference the sdcard as well.

like image 140
koljaTM Avatar answered Oct 17 '22 02:10

koljaTM