Android Room Pre-packaged database has an invalid schema error
Android Room Pre-packaged database has an invalid schema error
我第一次尝试 room,它没有按预期工作,而且我无法弄清楚问题是什么,因为我对 room 没有任何经验。任何帮助将非常感激。
我正在使用预打包的数据库,这是我用来创建数据库的查询。
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();
这就是我创建数据的方式class
@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
我在互联网上搜索了很多,但找不到任何可以帮助我的东西。
所以这里的任何帮助将不胜感激。谢谢
错误消息包含“预期”架构(您的 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
。
我第一次尝试 room,它没有按预期工作,而且我无法弄清楚问题是什么,因为我对 room 没有任何经验。任何帮助将非常感激。 我正在使用预打包的数据库,这是我用来创建数据库的查询。
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();
这就是我创建数据的方式class
@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
我在互联网上搜索了很多,但找不到任何可以帮助我的东西。 所以这里的任何帮助将不胜感激。谢谢
错误消息包含“预期”架构(您的 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
。