0

我想将 sqlite 数据库存储在 sd 卡中而不是本地存储,我在我的代码中尝试了示例,但没有帮助。我添加了我在下面使用的代码块,请帮助我在代码中缺少什么。谢谢如果我在这里有一个简单的逻辑,你投反对票。

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);
}

}

和..

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);
    }

}

4

1 回答 1

0

您可以在构造函数中将路径传递给数据库,例如:

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

变成

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

确保您对 SD 卡具有 R/W 权限,并且 SD 卡已安装。

于 2013-06-06T19:07:53.407 回答