在我的测试Android应用程序中,我打算创建并访问数据库文件,该文件将位于SD卡上。我在一个类的帮助下使用main活动,该类扩展了SQLiteOpenHelper。我想以与以前相同的方式使用它,但我必须以某种方式更改数据库PATH。你知道吗,如何实现它?
THX
我当前扩展SQLiteOpenHelper的类的代码:
public class DatabaseDefinition extends SQLiteOpenHelper{
private static final String DATABASE_NAME="test.db";
private static final int DATABASE_VERSION=1;
public DatabaseDefinition(Context context) {
super(context,DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_NAME+" ("+ _ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ NAME+" TEXT NOT NULL, " +SURNAME+" TEXT NOT NULL, "+PHONE+" INT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
我的主要代码:
public class DatabaseExampleActivity extends Activity {
private DatabaseDefinition database;
private static String[] FROM={_ID, NAME, SURNAME,PHONE};
private static String ORDER_BY=" DESC";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
database= new DatabaseDefinition(this);
try{
String name = null;
String surname=null;
int phone=0;
addEvent("John", "Black", 111012345);
}finally{
database.close();
}
}
}
答案 0 :(得分:4)
首先,您必须指定SD卡的路径。您可以通过创建如下字符串来实现:
public static final String DATABASE_FILE_PATH = "/sdcard";
但你应该致电
Environment.getExternalStorageDirectory()
获取SD卡的根路径并使用它创建数据库。之后,您可以根据需要创建数据库。这是一个例子
public class DatabaseHelper
{
private static final String TAG = "DatabaseHelper";
public static final String DATABASE_FILE_PATH = Environment.getExternalStorageDirectory();
public static final String DATABASE_NAME = "mydb";
public static final String TRACKS_TABLE = "tracks";
public static final String TRACK_INFO_TABLE = "track_info";
private static final String TRACKS_TABLE_CREATE = "create table "
+ TRACKS_TABLE
+ " (_id integer primary key autoincrement, title text not null, description text null, created_at date not null);";
private static final String TRACK_INFO_TABLE_CREATE = "create table "
+ TRACK_INFO_TABLE
+ " (_id integer primary key autoincrement, track_id integer not null, latitude real not null, longitude real not null, altitude real not null, created_at date not null);";
private SQLiteDatabase database;
public DatabaseHelper()
{
try
{
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
}
catch (SQLiteException ex)
{
Log.e(TAG, "error -- " + ex.getMessage(), ex);
// error means tables does not exits
createTables();
}
finally
{
DBUtil.safeCloseDataBase(database);
}
}
private void createTables()
{
database.execSQL(TRACKS_TABLE_CREATE);
database.execSQL(TRACK_INFO_TABLE_CREATE);
}
public void close()
{
DBUtil.safeCloseDataBase(database);
}
public SQLiteDatabase getReadableDatabase()
{
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READONLY);
return database;
}
public SQLiteDatabase getWritableDatabase()
{
database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
+ File.separator + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
return database;
}
最后,您必须在清单中设置权限,如下所示:android.permission.WRITE_EXTERNAL_STORAGE
答案 1 :(得分:0)
如果你们有些人想在sd卡上创建sqlite数据库,但你还是
想要继续使用扩展sqliteOpenHelper
的类,您可以访问this answer。
您只需添加另一个类,在您的类(扩展sqliteOpenHelper
)中在super()中实现它,然后更改新类的路径。
注意:如果您使用的是HC或ICS,则需要在路径中描述“external_sd”。因为HC和ICS仍会报告物理内置到设备中的存储