有什么方法可以使用我的代码获取内部存储的 SQLite 数据库?
Any way to get internal stored SQLite database using my code?
我正在尝试通过我的 Java 代码访问我早期存储的 .db
文件:
String path = "/data/data/" + this.getPackageName() + "/databases/" + DBHelper.DATABASE_NAME;
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(path, null);
这是 .db
文件存储的位置:
enter image description here
原始路径:/data/data/com.xxx.xxx/databases/database.db
但是在编译时,Android Studio 不断警告我:(DATABASE_NAME 引用 database.db)
E/SQLiteLog: (1) no such table: database.db
MainActivity.java
private void startMatching() {
DBHelper helper = new DBHelper(this);
SQLiteDatabase write = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME_ORIGINAL, original);
write.insert(DBHelper.TABLE_NAME, null, values);
}
DBHelper.java
public static final String DATABASE_NAME = "database.db";
public static final Integer DATABASE_VERSION = 1;
public static final String TABLE_NAME = "mTable";
public DBHelper(Context context) {
super(
context,
DATABASE_NAME,
null,
DATABASE_VERSION
);
}
我不知道为什么会出现这个错误。任何人都可以帮忙吗? :(
在android中,当你使用SqlLiteDatabase时,你并没有真正指定路径。所有这一切都为您处理。这里有一些很好的文档可以帮助您启动数据库:https://developer.android.com/training/data-storage/sqlite
简而言之,您的基本数据库 class 将如下所示:
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
请注意数据库名称只是一个名称,而不是路径。您还将有一个 'contract' class 来定义您的 table 常量,然后您可以在创建这些 table 时使用这些常量,以及 read/write 和来自那些 tables。通读链接教程,您应该可以开始了!
但是,如果您想使用 pre-populated 数据库,我建议您改用 SQLiteOpenHelper。
我正在尝试通过我的 Java 代码访问我早期存储的 .db
文件:
String path = "/data/data/" + this.getPackageName() + "/databases/" + DBHelper.DATABASE_NAME;
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(path, null);
这是 .db
文件存储的位置:
enter image description here
原始路径:/data/data/com.xxx.xxx/databases/database.db
但是在编译时,Android Studio 不断警告我:(DATABASE_NAME 引用 database.db)
E/SQLiteLog: (1) no such table: database.db
MainActivity.java
private void startMatching() {
DBHelper helper = new DBHelper(this);
SQLiteDatabase write = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME_ORIGINAL, original);
write.insert(DBHelper.TABLE_NAME, null, values);
}
DBHelper.java
public static final String DATABASE_NAME = "database.db";
public static final Integer DATABASE_VERSION = 1;
public static final String TABLE_NAME = "mTable";
public DBHelper(Context context) {
super(
context,
DATABASE_NAME,
null,
DATABASE_VERSION
);
}
我不知道为什么会出现这个错误。任何人都可以帮忙吗? :(
在android中,当你使用SqlLiteDatabase时,你并没有真正指定路径。所有这一切都为您处理。这里有一些很好的文档可以帮助您启动数据库:https://developer.android.com/training/data-storage/sqlite
简而言之,您的基本数据库 class 将如下所示:
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
请注意数据库名称只是一个名称,而不是路径。您还将有一个 'contract' class 来定义您的 table 常量,然后您可以在创建这些 table 时使用这些常量,以及 read/write 和来自那些 tables。通读链接教程,您应该可以开始了!
但是,如果您想使用 pre-populated 数据库,我建议您改用 SQLiteOpenHelper。