编译错误
解 : Entuty 加 getter & setter
Schema 错误提示
Warning: //此时build是成功的。
Error:(13, 17) 警告: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
解 [ref]https://stackoverflow.com/questions/44322178/room-schema-export-directory-is-not-provided-to-the-annotation-processor-so-we
//官网文档也有写的。
方案A exportSachma = false;
@Database(entities = { YourEntity.class }, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
...
}
方案B //看看大神的回答, 不说答案正确与否, 就这种代码注释值得学习。
----->>project-->>app--->>schemas---->>xxx.很长名字后面最后一个Database---->>会产生一个1.json
android {
// ... (compileSdkVersion, buildToolsVersion, etc)
defaultConfig {
// ... (applicationId, miSdkVersion, etc)
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
// ... (buildTypes, compileOptions, etc)
}
生成的Schema //关于Schema ,看官网
Schema JSON
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "559f8304933f593d9feffa68b452449b",
"entities": [
{
"tableName": "User",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER, `first_name` TEXT, `last_name` TEXT, PRIMARY KEY(`uid`))",
"fields": [
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER"
},
{
"fieldPath": "firstName",
"columnName": "first_name",
"affinity": "TEXT"
},
{
"fieldPath": "lastName",
"columnName": "last_name",
"affinity": "TEXT"
}
],
"primaryKey": {
"columnNames": [
"uid"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"559f8304933f593d9feffa68b452449b\")"
]
}
}
/data/data/databases 目录下没有数据库文件
解 : 此时还没有数据 insert ,所以没出来。 insert后会出现。
Debugging 模式卡住
解: 此时删除schema 文件就能正常Debugging //不知到原因。
数据写入不了 //看前边的代码 – 感谢 monroe ,北京-秦微明 @Android studio QQ 群 的提点。
解 : 因为 transaction 完成需要commit,标记事务成功 setTransactionSuccessful()
//这代码不能正常 insert why?
mDb.beginTransaction();
mDb.userDao().insertA(user);
mDb.endTransaction();
src //非常粗糙的代码
github
---------------------
作者:AndroLi
来源:CSDN
原文:https://blog.csdn.net/zkwsr/article/details/76578312?utm_source=copy
版权声明:本文为博主原创文章,转载请附上博文链接!