繁体   English   中英

SQLite 数据库没有被复制(没有这样的表存在错误)

[英]SQLite database not getting copied (No such a table exists error)

我认为我的数据库没有被复制,因为我的 assets 文件夹中的数据库文件是 32KB并且显示在设备文件资源管理器中的文件是 12KB 所以我尝试从设备文件资源管理器上传数据库文件然后没有错误。 请问有人可以帮我解决这个问题吗?

设备文件资源管理器中的数据库

这是我得到的错误

错误

这是我的数据库助手 class

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) {
        }
    }
}

这是我的数据库副本 class

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);

我怀疑复制的内容不是您所期望的。 12KB 连同消息(未找到表)表示两件事。 存在一个有效的数据库,并且它可能包含一些组件。 IE

首先要尝试的是卸载应用程序并重新运行以消除现有尝试留下有效但为空的组件(表等)数据库。

如果不能解决问题,那么我建议添加以下内容

DatabaseUtils.dumpCursor(databaseAdapter.db.rawQuery("SELECT * FROM sqlite_master",null));

行前的stationsList = databaseAdapter.getAllStations();

这将提取数据库中所有组件(表、视图触发器、索引)的列表,例如:-

2022-01-23 13:01:13.611 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@45ef147
2022-01-23 13:01:13.611 I/System.out: 0 {
2022-01-23 13:01:13.612 I/System.out:    type=table
2022-01-23 13:01:13.612 I/System.out:    name=android_metadata
2022-01-23 13:01:13.612 I/System.out:    tbl_name=android_metadata
2022-01-23 13:01:13.612 I/System.out:    rootpage=3
2022-01-23 13:01:13.612 I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2022-01-23 13:01:13.612 I/System.out: }
2022-01-23 13:01:13.612 I/System.out: <<<<<

即上面只有SQLiteOpenHelper 创建的android_metadata 表。

请注意,上面是使用您的助手创建的,没有调用 PreCreateDB,copyDB。 低矮的设备资源管理器显示:-

在此处输入图像描述

因此,它看起来不像 PreCreateDB.copyDB 正在复制文件,或者如果它可能已损坏,在这种情况下 SQLiteOpenHelper 将向您提供一个全新但空的数据库栏 android_metadata 表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM