在我的应用程序中,我使用sqlite数据库,该数据库当前正在手机内存中创建并可以在SD卡上创建数据库
public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="contiDB.db";
public static final String CONTI="turni";
public static final String CONTI_ID="idturno";
public static final String ANNO="anno";
public static final String MESE="mese";
public static final String DATA="data";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+CONTI+" ("+CONTI_ID+" INTEGER PRIMARY KEY,"+DATA+" NUMERIC,"+MESE+" TEXT,"+ANNO+" NUMERIC)";
db.execSQL(CREATE_TABLE);
答案 0 :(得分:1)
实际上,db是在/data/data/your.app.name/databases/your.db中创建的,
但您可以指定SD卡的路径
简而言之:
在您的清单文件中设置此权限:
<!-- To write and read to and from SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
获取SD卡的路径
public static final String DATABASE_FILE_PATH = Environment.getExternalStorageDirectory();
打开数据库(getWitableDatabase,getReadableDatabase)时,请指定路径:
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
或
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READONLY);
分别
<强> [编辑] 强>
请注意,WRITE_EXTERNAL_STORAGE包含READ_EXTERNAL_STORAGE,因此,无需另外指定。