【问题标题】:Android Room Pre-packaged database has an invalid schema errorAndroid Room 预打包的数据库存在无效架构错误
【发布时间】:2021-03-02 01:52:36
【问题描述】:

我第一次尝试了房间,但它没有按预期工作,我无法弄清楚问题是什么,因为我没有任何房间经验。任何帮助将非常感激。 我正在使用预打包的数据库,这是我用来创建数据库的查询。

CREATE TABLE quotes_table (
id          INTEGER PRIMARY KEY AUTOINCREMENT,
quote       TEXT    UNIQUE,
category_id TEXT    NOT NULL
);

这就是我在我的应用程序中创建数据库的方式

Room.databaseBuilder(context.getApplicationContext(),
                        AppDatabase.class, "quotes_database.db")
                        .allowMainThreadQueries()
                        .createFromAsset("database/quotes_database.db")
                        .fallbackToDestructiveMigration()
                        .build();

这就是我创建数据类的方式

@Entity(tableName = "quotes_table" ,
    indices = { @Index(value = {"quote"}, unique = true) } )
public class Quote {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private int id;

@ColumnInfo(name = "quote")
private String quote;

@NonNull
@ColumnInfo(name = "category_id")
private String categoryId;

但是当我启动应用程序时我的应用程序崩溃了,这是我的 logcat

2020-11-17 20:27:14.866 1588-1588/com.example.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 1588
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.IllegalStateException: Pre-packaged database has an invalid schema: quotes_table(com.example.test.Quote).
 Expected:
TableInfo{name='quotes_table', columns={category_id=Column{name='category_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, quote=Column{name='quote', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_quotes_table_quote', unique=true, columns=[quote]}]}
 Found:
TableInfo{name='quotes_table', columns={quote=Column{name='quote', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, category_id=Column{name='category_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_quotes_table_quote', unique=true, columns=[quote]}]}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6651)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
 Caused by: java.lang.IllegalStateException: Pre-packaged database has an invalid schema: quotes_table(com.example.test.Quote).
 Expected:
TableInfo{name='quotes_table', columns={category_id=Column{name='category_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, quote=Column{name='quote', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_quotes_table_quote', unique=true, columns=[quote]}]}
 Found:
TableInfo{name='quotes_table', columns={quote=Column{name='quote', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, category_id=Column{name='category_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_quotes_table_quote', unique=true, columns=[quote]}]}
    at androidx.room.RoomOpenHelper.onCreate(RoomOpenHelper.java:82)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onCreate(FrameworkSQLiteOpenHelper.java:118)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:333)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
    at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
    at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.java:90)
    at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476)
    at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281)
    at com.example.test.QuoteDao_Impl.getAllQuotes(QuoteDao_Impl.java:63)
2020-11-17 20:27:14.867 1588-1588/com.example.test E/AndroidRuntime:     at com.example.test.MainActivity.onCreate(MainActivity.java:44)
        at android.app.Activity.performCreate(Activity.java:7088)
        at android.app.Activity.performCreate(Activity.java:7079)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
            ... 9 more

我在互联网上进行了很多搜索,但找不到任何可以帮助我的东西。 所以这里的任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: android database sqlite android-sqlite android-room


    【解决方案1】:

    错误消息包含“预期”架构(您的 Room Entity 定义的内容)和“找到”架构 - 您的预打包数据库的架构。重新排序列以匹配给你:

     Expected:
    TableInfo{name='quotes_table', columns={category_id=Column{name='category_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true,  primaryKeyPosition=1, defaultValue='null'}, quote=Column{name='quote', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_quotes_table_quote', unique=true, columns=[quote]}]}
     Found:
    TableInfo{name='quotes_table', columns={category_id=Column{name='category_id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, quote=Column{name='quote', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_quotes_table_quote', unique=true, columns=[quote]}]}
    

    这些需要完全匹配。但是,您预期的架构有一个非空的 id 列:

    id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}
    

    但是您的预打包数据库有一个可以为空的id 列:

    id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}
    

    您需要更改您的预打包数据库以确保id 列是NOT NULL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-05
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多