Room公开了哪些SQLite方法?

时间:2017-12-15 12:18:02

标签: android sqlite android-room

我正在开发一个高度依赖database的应用程序。所以为此,我决定使用Room。但在开始之前,我很少关注我尝试但没有成功。

  1. 我可以在房间内更改页面和缓存大小吗?
  2. 空间可以NGQP吗?
  3. Vaccum可能吗?
  4. 我可以查询已创建的SQLite数据库吗?即我的SD卡上已有数据库,所以我可以直接在房间使用它吗?
  5. 我可以在房间里使用Pragma吗?

1 个答案:

答案 0 :(得分:0)

Re 2 - NGQP可以用空间吗?

我认为NGQP基本上依赖于SQLIte版本,如果你使用的是一个版本的SQLite,它应该可以工作,即3.8.0,所以API21 + BUT

  

某些设备制造商包含不同版本的SQLite   他们的设备。android.database.sqlite

This may also be of interest - The Next-Generation Query Planner

Re 4 - 我可以查询已经创建的SQLite数据库吗?即我的SD卡上已有数据库,所以我可以直接在房间使用它吗?

这可能是您感兴趣的How to migrate existing SQLite application to Room Persistance Library?

当然没有理由不在Room之外打开数据库,例如有一个SQLiteOpenHelper的子类。但是,我猜房间警察:)可能会提出很多理由,为什么你不应该这样做。

Re 1,3和5

我相信您可以覆盖扩展init类的类的RoomDatabase方法,这是RoomDatabase子类打开数据库之前的安全位置。

e.g。 : -

@Database(entities = Todo.class, version = 1, exportSchema = false)
public abstract class TodoDatabase extends RoomDatabase {
    public abstract TodoDAO todoDAO();

    @Override
    public void init(DatabaseConfiguration dbconfig) {
        Log.d("ROOM_INIT","Room init invoked - Databse name is " + dbconfig.name);
        String dbpath = (dbconfig.context).getDatabasePath(dbconfig.name).getPath();
        if (ifDBExists(dbpath)) {
            SQLiteDatabase db = SQLiteDatabase.openDatabase(dbpath, null,Context.MODE_PRIVATE);
            actionCustomConfiguration(db);
            db.close();
        }
        super.init(dbconfig);
    }

    private boolean ifDBExists(String dbpath) {
        File db = new File(dbpath);
        if(db.exists()) return true;
        File dir = new File(db.getParent());
        if (!dir.exists()) {
            dir.mkdirs();
        }
        return false;
    }

    private void actionCustomConfiguration(SQLiteDatabase db) {
        Log.d("ROOM_CUSTCONFIG","Custom Configuration invoked.");
        db.execSQL("VACUUM");
        logDatabaseInfo(db);
    }

    private static void logDatabaseInfo(SQLiteDatabase db) {

        // Issue PRAGMA database_list commnand
        Cursor dblcsr = db.rawQuery(PRAGMA_STATEMENT + PRAGMA_DATABASELIST,null);
        // Write databases to the log
        while (dblcsr.moveToNext()) {
            Log.d(CSU_TAG,"DatabaseList Row " + Integer.toString(dblcsr.getPosition() + 1) +
                    " Name=" + dblcsr.getString(dblcsr.getColumnIndex(PRAGMA_DBLIST_NAME_COL)) +
                    " File=" + dblcsr.getString(dblcsr.getColumnIndex(PRAGMA_DBLIST_FILE_COL))
            );
        }
        dblcsr.close();
        // Issue PRAGMA user_version to get the version and write to the log
        //Note! to set user_version use execSQL not rawQuery
        Cursor uvcsr = db.rawQuery(PRAGMA_STATEMENT + PRAGMA_USERVERSION,null);
        while (uvcsr.moveToNext()) {
            Log.d(CSU_TAG,"Database Version = " +
                    Integer.toString(uvcsr.getInt(uvcsr.getColumnIndex(PRAGMA_USERVERSION))));
        }
        uvcsr.close();
        // Select all table entry rows from sqlite_master
        Cursor tlcsr = db.rawQuery("SELECT * FROM " +
                        SQLITE_MASTER + " WHERE " +
                        SM_TABLE_TYPE_COLUMN + "='" + SM_TYPE_TABLE + "'"
                ,null);
        // For each table write table information to the log
        // (inner loop gets column info per table)
        while (tlcsr.moveToNext()) {
            String current_table = tlcsr.getString(tlcsr.getColumnIndex(SM_TABLENAME_COLUMN));
            Log.d(CSU_TAG,
                    "Table Name = " + current_table +
                            " Created Using = " + tlcsr.getString(tlcsr.getColumnIndex(SM_SQL_COLUMN)),
                    null
            );
            // Issue PRAGMA tabel_info for the current table
            Cursor ticsr = db.rawQuery(PRAGMA_STATEMENT + PRAGMA_TABLEINFO +
                            "(" + current_table + ")",
                    null
            );
            // Write column info (see headings below) to the log
            while (ticsr.moveToNext()) {
                Log.d(CSU_TAG,"Table = " +
                                current_table +
                                " ColumnName = " +
                                ticsr.getString(ticsr.getColumnIndex(PRAGMA_TABLEINFO_NAME_COl)) +
                                " ColumnType = " +
                                ticsr.getString(ticsr.getColumnIndex(PRAGMA_TABLEINFO_TYPE_COL)) +
                                " Default Value = " +
                                ticsr.getString(ticsr.getColumnIndex(PRAGMA_TABLEINFO_DEFAULTVALUE_COL)) +
                                " PRIMARY KEY SEQUENCE = " + Integer.toString(
                        ticsr.getInt(ticsr.getColumnIndex(PRAGMA_TABLEINFO_PRIMARYKEY_COL))
                        )
                );
            }
            ticsr.close();
        }
        tlcsr.close();
    }
}

上述输出示例: -

12-18 02:21:33.157 1607-1607/mjt.roomtodo D/ROOM_INIT: Room init invoked - Databse name is tododb
12-18 02:21:33.157 1607-1607/mjt.roomtodo D/ROOM_CUSTCONFIG: Custom Configuration invoked.
12-18 02:21:33.165 1607-1607/mjt.roomtodo D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/mjt.roomtodo/databases/tododb
12-18 02:21:33.165 1607-1607/mjt.roomtodo D/SQLITE_CSU: Database Version = 1
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table Name = room_master_table Created Using = CREATE TABLE room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = room_master_table ColumnName = id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = room_master_table ColumnName = identity_hash ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table Name = Todo Created Using = CREATE TABLE `Todo` (`_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `description` TEXT, `starts` INTEGER NOT NULL, `finishes` INTEGER NOT NULL, `status_flags` INTEGER NOT NULL, `repeat_code` INTEGER NOT NULL, `repeat_multiplier` INTEGER NOT NULL, `added_count` INTEGER NOT NULL)
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = _id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = description ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = starts ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = finishes ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.169 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = status_flags ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.173 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = repeat_code ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.173 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = repeat_multiplier ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.173 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = Todo ColumnName = added_count ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.173 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table Name = sqlite_sequence Created Using = CREATE TABLE sqlite_sequence(name,seq)
12-18 02:21:33.173 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = sqlite_sequence ColumnName = name ColumnType =  Default Value = null PRIMARY KEY SEQUENCE = 0
12-18 02:21:33.173 1607-1607/mjt.roomtodo D/SQLITE_CSU: Table = sqlite_sequence ColumnName = seq ColumnType =  Default Value = null PRIMARY KEY SEQUENCE = 0