我正在尝试使用Room的createFromAsset
方法从资产
文件夹中复制预填充的SQLite数据库。数据库相当大(一个表有~370k个条目;大约18mb的总大小)。数据库在数据库
文件夹中创建,它包含适用的表,但没有数据传输到表中。
我已经复习了以下SO问题,但还没有找到解决方案:
我还查看了以下信息页面:
我尝试等待几分钟,看看数据库填充是否需要时间,但没有数据出现。我一直在使用AS的设备文件资源管理器从虚拟设备下载数据库,并在SQLiteDB浏览器中打开它,以及使用AS的数据库检查器。
此外,LogCat始终显示以下内容(每次在MainActivity中记录):
D/MainActivity: onCreate前10个未找到的好词
表1-"bad_words"
列1-"word", TEXT
表2-"english_words"
列1-"id",INTEGER
列2-"word",TEXT
列3-"频率",INTEGER
app build. gradle依赖项:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.13.1'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
// For Room database
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
testImplementation "androidx.room:room-testing:$room_version"
}
ValidWords实体:
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity(tableName = "english_words")
public class ValidWords {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
public int mID;
@ColumnInfo(name = "word")
@NonNull
public String mWord;
@ColumnInfo(name = "frequency")
public int mFrequency;
public ValidWords(@NonNull String word) {this.mWord = word;}
public String getWord() {return this.mWord;}
public void setWord(@NonNull String word) {this.mWord = word;}
public int getFrequency() {return mFrequency;}
public void setFrequency(int frequency) {this.mFrequency = frequency;}
}
BadWords实体:
import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity (tableName = "bad_words")
public class BadWords {
@PrimaryKey
@NonNull
@ColumnInfo(name = "word")
public String mWord;
public BadWords(@NonNull String word) {this.mWord = word;}
@NonNull
public String getWord() {return mWord;}
public void setWord(@NonNull String mWord) {this.mWord = mWord;}
}
wordDAO接口:
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import java.util.List;
@Dao
public interface wordsDAO {
@Query("SELECT * FROM english_words LIMIT 10")
List<ValidWords> getFirstTenGoodWords();
@Query("SELECT * FROM bad_words")
List<BadWords> getBadWords();
@Query("SELECT * FROM english_words WHERE word LIKE :wordToCheck LIMIT 1")
ValidWords getMatch(String wordToCheck);
@Insert(entity = ValidWords.class)
long insertGoodWordInfo(ValidWords validWord);
}
参考WordsDB数据库:
import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@Database(entities = {BadWords.class, ValidWords.class}, version = 2)
public abstract class ReferenceWordsDB extends RoomDatabase {
public abstract wordsDAO wordsDAO();
private static volatile ReferenceWordsDB INSTANCE;
static ReferenceWordsDB getDatabase(final Context context){
if (INSTANCE == null){
synchronized (ReferenceWordsDB.class){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
ReferenceWordsDB.class, "GoodWordsAndFrequency.db")
.createFromAsset("GoodWordsAndFrequency.db")
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
}
}
}
return INSTANCE;
}
}
主要活动:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.res.ResourcesCompat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static ArrayList<ArrayList<ValidWords>> infoFromFindActualWordRunnableClass;
private final String LOG_CLASS = "MainActivity";
private ReferenceWordsDB wordsDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
final String METHOD = "onCreate";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViews();
wordsDB = ReferenceWordsDB.getDatabase(this);
List<ValidWords> first10GoodWords = new ArrayList<>(wordsDB.wordsDAO().getFirstTenGoodWords());
if (first10GoodWords.size() == 0){
Log.d(LOG_CLASS, String.format(Locale.US, "%s 1st 10 good words NOT FOUND", METHOD));
}
for (ValidWords currentWord : first10GoodWords){
Log.d(LOG_CLASS, String.format(Locale.US, "%s currentWord word = %s, frequency = %d", METHOD, currentWord.getWord(), currentWord.getFrequency()));
}
...
}
}
我正在使用安装了所有已知更新的Android Studio 4.2.2。
据猜测,可能的问题是数据库正在被破坏,因为迁移不存在与使用结合使用的. ellbackToDestructive迁移()
,因此是空的但其他有效的数据库。
因此,您应该检查源数据库的版本(user_versionEdit Pragmas for Db Browesr)。它必须与@Database中代码中的版本匹配。在您的情况下,您有@Database(实体={BadWords. class,ValidWords.class},version=2)
,因此浏览器DBuser_versionid应该是2。