SQLiteOpenHelper - 在 SD 卡上创建数据库
在我的测试 Android 应用程序中,我打算创建并访问数据库文件,该文件将位于 SD 卡上。我正在一个类的帮助下使用主要活动,该类扩展了 SQLiteOpenHelper。我想像以前一样使用它,但我必须以某种方式更改数据库路径。你知道如何实现吗?
谢谢
我当前扩展 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();
}
}
}
in my test android app I intend to create and access database file, which will be located on SD card. I am using main activity with help of a class, which extends SQLiteOpenHelper. I want to use it the same way as before, but I have to somehow change the database PATH. Do you know, how to achieve it?
thx
My current code of a class which extends 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);
}
And code of my main:
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须指定 sdcard 的路径。您可以通过创建如下字符串来做到这一点:
但是您应该调用
以获取 SD 卡的根路径并使用它来创建数据库。之后,您可以根据需要创建数据库。这是一个示例
,最后您必须在清单中设置权限,如下所示:
android.permission.WRITE_EXTERNAL_STORAGE
祝你好运:)
阿克德
First you have to specify the path of the sdcard. You can do that by creating a string like this:
But for you should call
to get the root path to the SD card and use that to create the database. After that you create the database as you want. Here is an example
And in the end you have to set permission in manifest like this:
android.permission.WRITE_EXTERNAL_STORAGE
Good luck :)
Arkde
如果你们中的一些人想在 SD 卡上创建 sqlite 数据库,但您仍然
想要继续使用扩展
sqliteOpenHelper
的类,您可以访问此答案。您只需添加另一个类,在 super() 中的类(扩展了 sqliteOpenHelper )上实现它,然后更改新类的路径。
注意:如果您使用 HC 或 ICS,则需要在路径上描述“external_sd”。因为 HC 和 ICS 仍会报告设备中物理内置的存储
If some of you want to create sqlite database on sd card but you still
want to keep using class which extends
sqliteOpenHelper
, you can visit this answer.You just have to add another class, implement it on your class (which extends
sqliteOpenHelper
) in super(), then change the path on your new class.Note: If you are using HC or ICS you need to describe the "external_sd" on your path. Because HC and ICS will still report storage that's physically built into the device