简体   繁体   中英

How to store sqlite database in sdcard in android?

I would like to store sqlite database in sd card instead of storing locally,I tried examples in my code but it didn't help.I have added block of code I have used below,kindly help on what I am missing on code.Thank you and down vote if I had a simple logic here.

public class ListSparesRegistrationOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "LIST_List_DB";
public static final String TABLE_NAME = "LIST_List_TABLE";
public static final int VERSION = 1;
public static final String KEY_ID = "_id";
public static final String ID = "ID";
public static final String MATERIAL = "Material";


public static final String SCRIPT = "create table " + TABLE_NAME + " ("
        + KEY_ID + " integer primary key autoincrement, " 
        + ID+ " text not null, " 
        + MATERIAL + " text not null );";

public ListSparesRegistrationOpenHelper(Context context, String name,
        CursorFactory factory, int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(SCRIPT);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("drop table " + TABLE_NAME);
    onCreate(db);
}

}

And..

public class ListSparesRegistrationAdapter {
SQLiteDatabase database_ob;
ListSparesRegistrationOpenHelper openHelper_ob;
Context context;

public ListSparesRegistrationAdapter(Context c) {
    context = c;
}

public ListSparesRegistrationAdapter opnToRead() {
    openHelper_ob = new ListSparesRegistrationOpenHelper(context,
            openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
    database_ob = openHelper_ob.getReadableDatabase();
    return this;

}

public ListSparesRegistrationAdapter opnToWrite() {
    openHelper_ob = new ListSparesRegistrationOpenHelper(context,
            openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
    database_ob = openHelper_ob.getWritableDatabase();
    return this;

}

public Cursor getAllTitles()
{
      String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.ID,
              openHelper_ob.MATERIAL};
      opnToWrite();
      Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
              null, null, null, null, null);

      return c;
}

public void Close() {
    database_ob.close();
}


public long insertDetails(String id, String material) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(openHelper_ob.ID, id);
    contentValues.put(openHelper_ob.MATERIAL, material);


    opnToWrite();
    long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
            contentValues);
    Close();
    return val;

}

public Cursor queryName() {
    String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.ID,
            openHelper_ob.MATERIAL};
    opnToWrite();
    Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null,
            null, null, null, null);

    return c;

}

public Cursor queryAll(int nameId) {
    String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.ID,
            openHelper_ob.MATERIAL};
    opnToWrite();
    Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
            openHelper_ob.KEY_ID + "=" + nameId, null, null, null, null);

    return c;

}



public long updateldetail(int rowId, String id, String material) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(openHelper_ob.ID, id);
    contentValues.put(openHelper_ob.MATERIAL, material);


    opnToWrite();
    long val = database_ob.update(openHelper_ob.TABLE_NAME, contentValues,
            openHelper_ob.KEY_ID + "=" + rowId, null);
    Close();
    return val;
}

public int deletOneRecord(int rowId) {
    // TODO Auto-generated method stub
    opnToWrite();
    int val = database_ob.delete(openHelper_ob.TABLE_NAME,
            openHelper_ob.KEY_ID + "=" + rowId, null);
    Close();
    return val;
}

public int deleteAll(){
    opnToWrite();
     return database_ob.delete(openHelper_ob.TABLE_NAME, null, null);
    }

}

You can pass the path to the database in the constructor, such as:

openHelper_ob = new ListSparesRegistrationOpenHelper(context,
        openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);

becomes

String path = getExternalStorageDirectory() + "myPath/";
openHelper_ob = new ListSparesRegistrationOpenHelper(context,
        path + openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);

Be sure you have R/W permissions on the SD card, and that the SD card is even mounted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM