Why database is null, when i use createFromAsset in RoomDatabase. List<> from it has size 0, but prepopulated database has 8 items

huangapple go评论78阅读模式
英文:

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 = &quot;words&quot;;
        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(&quot;words.db&quot;)
                        .allowMainThreadQueries()
                        .build();
                Log.i(&quot;1111&quot;, &quot;database was created now&quot;);
            } else {
                Log.i(&quot;1111&quot;, &quot;was already created&quot;);
            }}
        return database;
    }
    public abstract WordsDao wordsDao();

This is my Dao

@Dao
public interface WordsDao {
    @Query(&quot;SELECT * FROM pronouns&quot;)
    List&lt;Word&gt; getAllWords();
    @Query(&quot;SELECT * FROM pronouns WHERE wordRus == :wordRus&quot;)
    Word getWordByWord(String wordRus);

This is my Entity

@Entity(tableName = &quot;pronouns&quot;)
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(&quot;я&quot;);
        Log.i(&quot;1111&quot;, word.getWordRus());

This is my log

I/1111: database was created now
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 

&#39;java.lang.String com.vazheninapps.hindipoliglot.data.Word.getWordRus()&#39; 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=&#39;pronouns&#39;, columns={progress=Column{name=&#39;progress&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=true, primaryKeyPosition=0, defaultValue=&#39;null&#39;}, wordRus=Column{name=&#39;wordRus&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}, position=Column{name=&#39;position&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=true, primaryKeyPosition=1, defaultValue=&#39;null&#39;}, wordHin=Column{name=&#39;wordHin&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}, wordDev=Column{name=&#39;wordDev&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}}, foreignKeys=[], indices=[]}
         Found:
        TableInfo{name=&#39;pronouns&#39;, columns={wordRus=Column{name=&#39;wordRus&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}, wordHin=Column{name=&#39;wordHin&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}, progress=Column{name=&#39;progress&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}, position=Column{name=&#39;position&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=true, primaryKeyPosition=1, defaultValue=&#39;null&#39;}, wordDev=Column{name=&#39;wordDev&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}}, foreignKeys=[], indices=[]}

========

progress=Column{name=&#39;progress&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=true, primaryKeyPosition=0, defaultValue=&#39;null&#39;}
progress=Column{name=&#39;progress&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}

wordRus=Column{name=&#39;wordRus&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}
wordRus=Column{name=&#39;wordRus&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}

position=Column{name=&#39;position&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=true, primaryKeyPosition=1, defaultValue=&#39;null&#39;}
position=Column{name=&#39;position&#39;, type=&#39;INTEGER&#39;, affinity=&#39;3&#39;, notNull=true, primaryKeyPosition=1, defaultValue=&#39;null&#39;}

wordHin=Column{name=&#39;wordHin&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}
wordHin=Column{name=&#39;wordHin&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}

wordDev=Column{name=&#39;wordDev&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}}, foreignKeys=[], indices=[]}
wordDev=Column{name=&#39;wordDev&#39;, type=&#39;TEXT&#39;, affinity=&#39;2&#39;, notNull=false, primaryKeyPosition=0, defaultValue=&#39;null&#39;}}, 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

huangapple
  • 本文由 发表于 2020年4月4日 12:18:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61023703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定