英文:
Why database is null, when i use createFromAsset in RoomDatabase. List<> from it has size 0, but prepopulated database has 8 items
问题
以下是翻译好的部分:
这是我的数据库。
@Database(entities = {Word.class}, version = 1, exportSchema = false)
public abstract class WordsDatabase extends RoomDatabase {
private static final String DB_NAME = "words";
private static WordsDatabase database;
private static final Object LOCK = new Object();
public static WordsDatabase getInstance(Context context) {
synchronized (LOCK) {
if (database == null) {
database = Room.databaseBuilder(context, WordsDatabase.class, DB_NAME)
.createFromAsset("words.db")
.allowMainThreadQueries()
.build();
Log.i("1111", "数据库已创建");
} else {
Log.i("1111", "已经创建过");
}
}
return database;
}
public abstract WordsDao wordsDao();
}
这是我的数据访问对象(Dao)。
@Dao
public interface WordsDao {
@Query("SELECT * FROM pronouns")
List<Word> getAllWords();
@Query("SELECT * FROM pronouns WHERE wordRus == :wordRus")
Word getWordByWord(String wordRus);
}
这是我的实体(Entity)。
@Entity(tableName = "pronouns")
public class Word {
@PrimaryKey
private int position;
private String wordRus;
private String wordHin;
private String wordDev;
private int progress;
// 包含所有字段的构造函数、getter 和 setter...
}
这是我的 onCreate()
方法。
database = WordsDatabase.getInstance(this);
Word word = database.wordsDao().getWordByWord("я");
Log.i("1111", word.getWordRus());
这是我的日志。
I/1111: 数据库已创建
Caused by: java.lang.NullPointerException: 尝试调用虚拟方法
'java.lang.String com.vazheninapps.hindipoliglot.data.Word.getWordRus()' 时发生空对象引用
这是我的 words.db
文件在资产中(SQL2 数据库)。
您在做什么错误?为什么数据库为 null?我还尝试过 getAllWords()
方法,并返回列表的大小。大小为 0。
英文:
This is my Database.
@Database(entities = {Word.class}, version = 1, exportSchema = false)
public abstract class WordsDatabase extends RoomDatabase {
private static final String DB_NAME = "words";
private static WordsDatabase database;
private static final Object LOCK = new Object();
public static WordsDatabase getInstance(Context context) {
synchronized (LOCK){
if(database == null) {
database = Room.databaseBuilder(context,WordsDatabase.class, DB_NAME)
.createFromAsset("words.db")
.allowMainThreadQueries()
.build();
Log.i("1111", "database was created now");
} else {
Log.i("1111", "was already created");
}}
return database;
}
public abstract WordsDao wordsDao();
This is my Dao
@Dao
public interface WordsDao {
@Query("SELECT * FROM pronouns")
List<Word> getAllWords();
@Query("SELECT * FROM pronouns WHERE wordRus == :wordRus")
Word getWordByWord(String wordRus);
This is my Entity
@Entity(tableName = "pronouns")
public class Word {
@PrimaryKey
private int position;
private String wordRus;
private String wordHin;
private String wordDev;
private int progress;
1 constructor with all fields, getters, setters...
this is my onCreate()
database = WordsDatabase.getInstance(this);
Word word = database.wordsDao().getWordByWord("я");
Log.i("1111", word.getWordRus());
This is my log
I/1111: database was created now
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String com.vazheninapps.hindipoliglot.data.Word.getWordRus()' on a
null object reference
This is my words.db in assets(SQL2 database) SQLSTUDIOIMAGE
what i am doing wrong? Why database null? I also try method getAllWords(). and return size of list. it is 0.
答案1
得分: 1
我找到了答案。
首先,我将 SQL2 改为 SQL3,从设备中删除应用程序并重新安装应用程序。现在的日志是:
java.lang.IllegalStateException: 预打包的数据库具有无效的模式:代词
期望:
TableInfo{name='pronouns', columns={progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
发现:
TableInfo{name='pronouns', columns={wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, progress=Column{name='progress', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}
progress=Column{name='progress', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}
position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}
wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
期望和实际结果的顺序不同,但这无关紧要。重要的是在期望中 NotNull 为 true,在实际结果中 Notnull 为 false。我所做的一切就是在预填充数据库中将 position 和 progress 的 NotNull 设置为 true,以便实现 true=true 的匹配。
英文:
I found answer.
First, I made SQL3 instead SQL 2, delete app from device and install application. Now Log is:
java.lang.IllegalStateException: Pre-packaged database has an invalid schema: pronouns
Expected:
TableInfo{name='pronouns', columns={progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='pronouns', columns={wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, progress=Column{name='progress', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
========
progress=Column{name='progress', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}
progress=Column{name='progress', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordRus=Column{name='wordRus', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}
position=Column{name='position', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}
wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordHin=Column{name='wordHin', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}
wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
wordDev=Column{name='wordDev', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
There are expected and found in wrong order, but it doesn't matter. Matter that where in expected NotNull true and in Found Notnull false. All I done is added to position and progess notNull true in prepopulated database, for match true=true.
答案2
得分: 0
在我的情况下
简单解决方法:在手机上:长按应用->应用信息->存储与内存->清除存储
英文:
In my case
Simple solve: On phone: long touch on app->App Info->Storage &memory-> Clear Storage
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论