Monty
Monty

Reputation: 3215

android SQLite database in sdcard

I want to know one thing in android sqlite.

I created my database in sdcard. so there is no `upgrage() method.

public class DatabaseClass
{
    public static final String  DATABASE_FILE_PATH = "/sdcard";
    public static final String  DATABASE_NAME = "testDatabase";

    public static final String TRACKS_TABLE_CREATE =
        "create table if not exists casecategory (_id INTEGER primary key autoincrement, "
        + "category_name TEXT not null);";

    public SQLiteDatabase database;

    public DatabaseClass()
    {  
        try
        {
            database = SQLiteDatabase.openOrCreateDatabase(DATABASE_FILE_PATH + File.separator + DATABASE_NAME, null);
            createTables();
        }

        catch (SQLiteException ex)
        {
            Log.e("Hello", "error -- " + ex.getMessage(), ex);
        }
        finally
        {
            database.close();
        }
    }

in internal memory we create like :-

public class DatabaseClass extends SQLiteOpenHelper 
{
    /*Constants */
    public final static String DATABASE_NAME ="localdbfile.db";
    public final static String NAME ="name";
    public final static String ADDRESS ="address";


    public final static String CITY ="city";

    public DatabaseClass(Context context) {
        super(context, DATABASE_NAME, null, 1);
        // TODO Auto-generated constructor stub
        }

    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL( "CREATE TABLE mylistdata(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, address TEXT,city TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP Table mylistdata");
        onCreate(db);

    }

}

in internal memory we change the version number and upgrade() called. which upgrade the DB.

if i want to upgrade database version in sdcard database.

how can i do this ,if possible.

Upvotes: 2

Views: 9665

Answers (1)

Shiv
Shiv

Reputation: 4567

You can store your database directly in SD card like this:

static class DatabaseClass extends SQLiteOpenHelper {

    DatabaseClass(final Context context) {
        super(context, Environment.getExternalStorageDirectory()
                + File.separator + "/DataBase/" + File.separator
                + DATABASE_NAME, null, DATABASE_VERSION);
    }

If you store ur databse in internal memory then you can also copy the database from internal memory to SD card :)

Here's an example how to copy data to SD card:

InputStream myInput = new FileInputStream("/data/data/pack_name/databases/databasename.db");

File directory = new File("/sdcard/some_folder");
if (!directory.exists()) {
    directory.mkdirs();
}

OutputStream myOutput = new FileOutputStream(directory.getPath() + "/AppName.backup");

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

Upvotes: 8

Related Questions