【发布时间】:2022-01-23 01:16:45
【问题描述】:
我认为我的数据库没有被复制,因为我的 assets 文件夹中的数据库文件为 32KB,而 设备文件资源管理器中显示的文件为 12KB。所以我尝试从设备文件资源管理器上传数据库文件,然后没有错误。请问有人可以帮我解决这个问题吗?
这是我遇到的错误
这是我的 DatabaseHelper 类
public class DatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
List<Contacts> contactsList = new ArrayList<>();
List<Station> stationsList = new ArrayList<>();
List<Tip> tipsList = new ArrayList<>();
public DatabaseAdapter(Context context) {
helper = new DatabaseHelper(context);
db = helper.getWritableDatabase();
}
public List<Contacts> getAllContacts() {
String[] columns = {DatabaseHelper.KEY_NAME, DatabaseHelper.KEY_POSITION, DatabaseHelper.KEY_STATION, DatabaseHelper.KEY_PHONE_NUMBER, DatabaseHelper.KEY_EMAIL};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME1, columns, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int index2 = cursor.getColumnIndex(DatabaseHelper.KEY_NAME);
String name = cursor.getString(index2);
int index3 = cursor.getColumnIndex(DatabaseHelper.KEY_POSITION);
String position = cursor.getString(index3);
int index4 = cursor.getColumnIndex(DatabaseHelper.KEY_STATION);
String station = cursor.getString(index4);
int index5 = cursor.getColumnIndex(DatabaseHelper.KEY_PHONE_NUMBER);
String phone_number = cursor.getString(index5);
int index6 = cursor.getColumnIndex(DatabaseHelper.KEY_EMAIL);
String email = cursor.getString(index6);
Contacts contact = new Contacts(name, position, station, phone_number, email);
contactsList.add(contact);
}
return contactsList;
}
public List<Station> getAllStations() {
String[] columns = {DatabaseHelper.KEY_S_NAME, DatabaseHelper.KEY_S_ADDRESS, DatabaseHelper.KEY_S_PHONE_NUMBER, DatabaseHelper.KEY_S_TOTAL_FIGHTERS, DatabaseHelper.KEY_S_TOTAL_VEHICLES, DatabaseHelper.KEY_S_LAT, DatabaseHelper.KEY_S_LON};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME2, columns, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int index1 = cursor.getColumnIndex(DatabaseHelper.KEY_S_NAME);
String name = cursor.getString(index1);
int index2 = cursor.getColumnIndex(DatabaseHelper.KEY_S_ADDRESS);
String address = cursor.getString(index2);
int index3 = cursor.getColumnIndex(DatabaseHelper.KEY_S_PHONE_NUMBER);
String phone_number = cursor.getString(index3);
int index4 = cursor.getColumnIndex(DatabaseHelper.KEY_S_TOTAL_FIGHTERS);
int total_f = cursor.getInt(index4);
int index5 = cursor.getColumnIndex(DatabaseHelper.KEY_S_TOTAL_VEHICLES);
int total_v = cursor.getInt(index5);
int index6 = cursor.getColumnIndex(DatabaseHelper.KEY_S_LAT);
String lat = cursor.getString(index6);
int index7 = cursor.getColumnIndex(DatabaseHelper.KEY_S_LON);
String lon = cursor.getString(index7);
Station station = new Station(name, address, phone_number, total_f, total_v, lat, lon);
stationsList.add(station);
}
return stationsList;
}
public List<Tip> getAllTips() {
String[] columns = {DatabaseHelper.KEY_TIP_TOPIC, DatabaseHelper.KEY_TIP_SUB_TOPIC};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME3, columns, null, null, null, null, null, null);
while (cursor.moveToNext()) {
int index2 = cursor.getColumnIndex(DatabaseHelper.KEY_TIP_TOPIC);
String topic = cursor.getString(index2);
int index3 = cursor.getColumnIndex(DatabaseHelper.KEY_TIP_SUB_TOPIC);
String sub_topic = cursor.getString(index3);
Tip tip = new Tip(topic, sub_topic);
tipsList.add(tip);
}
return tipsList;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Firefighters.db";
private static final String TABLE_NAME1 = "contact_list";
private static final String TABLE_NAME2 = "station_list";
private static final String TABLE_NAME3 = "tips";
private static final int DATABASE_VERSION = 2;
private static final String KEY_NAME = "name";
private static final String KEY_POSITION = "position";
private static final String KEY_STATION = "station";
private static final String KEY_PHONE_NUMBER = "phone_number";
private static final String KEY_EMAIL = "email";
private static final String KEY_S_NAME = "name";
private static final String KEY_S_ADDRESS = "address";
private static final String KEY_S_PHONE_NUMBER = "phone_number";
private static final String KEY_S_TOTAL_FIGHTERS = "total_fighters";
private static final String KEY_S_TOTAL_VEHICLES = "total_vehicles";
private static final String KEY_S_LAT = "lat";
private static final String KEY_S_LON = "long";
private static final String KEY_TIP_TOPIC = "tip_topic";
private static final String KEY_TIP_SUB_TOPIC = "tip_sub_topic";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
db.disableWriteAheadLogging();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
这是我的数据库副本类
public class PreCreateDB {
public static void copyDB(Context context){
try{
String destPath = "/data/data/"+ context.getPackageName()
+ "/databases";
File f = new File(destPath);
if(!f.exists()){
f.mkdir();
rawCopy(context.getAssets().open("Firefighters.db"), new FileOutputStream(destPath + "/Firefighters.db"));
}
} catch (IOException e){
e.printStackTrace();
}
}
public static void rawCopy(InputStream inputStream, OutputStream outputStream) throws IOException{
byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
我就是这样称呼他们的
PreCreateDB.copyDB(mContext);
databaseAdapter = new DatabaseAdapter(mContext);
stationsList = databaseAdapter.getAllStations();
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(layoutManager);
stationsAdapter = new StationsAdapter(mContext, stationsList, recyclerView);
recyclerView.setAdapter(stationsAdapter);
-- 对我有用的解决方案--
我尝试了所有方法,但似乎都不起作用,所以我尝试了这个 SQLiteAssetHelper 库,它成功了!我所做的只是用 SQLiteAssetHelper 扩展数据库助手。这是我现在的数据库助手。
private static class DatabaseHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "Firefighters.db";
private static final String TABLE_NAME1 = "contact_list";
private static final String TABLE_NAME2 = "station_list";
private static final String TABLE_NAME3 = "tips";
private static final int DATABASE_VERSION = 2;
private static final String KEY_NAME = "name";
private static final String KEY_POSITION = "position";
private static final String KEY_STATION = "station";
private static final String KEY_PHONE_NUMBER = "phone_number";
private static final String KEY_EMAIL = "email";
private static final String KEY_S_NAME = "name";
private static final String KEY_S_ADDRESS = "address";
private static final String KEY_S_PHONE_NUMBER = "phone_number";
private static final String KEY_S_TOTAL_FIGHTERS = "total_fighters";
private static final String KEY_S_TOTAL_VEHICLES = "total_vehicles";
private static final String KEY_S_LAT = "lat";
private static final String KEY_S_LON = "long";
private static final String KEY_TIP_TOPIC = "tip_topic";
private static final String KEY_TIP_SUB_TOPIC = "tip_sub_topic";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
【问题讨论】:
-
如果在收盘前添加
outputStream.flush();会发生什么?您确定数据库中的表名匹配表名吗?我建议在stationsList = databaseAdapter.getAllStations();之前运行基于 SELECT * FROM sqlite_master 的查询;并使用 DatabaseUtils dumpCursor 方法检查哪些组件构成了复制的数据库。 developer.android.com/reference/android/database/… -
是的,我没有发现任何表名不匹配,当我从设备文件资源管理器上传数据库时,它运行良好,就像所有数据都被读取和显示一样,但我会尝试添加 outputStream.flush() ;
-
发现了问题,数据库文件夹/目录存在,因此不尝试复制并创建新的空表(没有您的表)。因此找不到表。使用建议的 PreCreateDB.copyDB 方法更正了答案,但保留了其他答案,因为它们可能会有所帮助。
标签: java android sqlite android-sqlite