Tag: SQLite

如何在Android的RoomDB中保存嵌套的List <String>

嘿谷歌有一个使用@Relation的例子 @Entity public class Pet { int userId; String name; // other fields } public class UserNameAndAllPets { public int id; public String name; @Relation(parentColumn = "id", entityColumn = "userId") public List<Pet> pets; } 是否可以保存字符串的列表,而不创建额外的类。 我想避免我的JsonProperty和一个房间实体之间的incosis W想有这样的东西 public class UserNameAndAllPets { @JsonProperty("id") public int id; @JsonProperty("name") public String name; @Relation(parentColumn = "id") @JsonProperty("pets") public List<String> […]

在Room数据库库中将Enum作为主键时出错

我正在使用一个枚举成为与数据库的TypeConvertor的主键和它dos't运行时编译并抛出一个错误 这是我的转换器 class QuranIndexConverter { @TypeConverter fun toQuranIndex(index: String): QuranIndex { return QuranIndex.valueOf(index) } @TypeConverter fun toStringIndex(quranIndex: QuranIndex): String { return quranIndex.name }} 这里是道 @Dao interface SuraDao { @Query("SELECT * FROM " + Constants.TABLE_NAME) fun getAll(): LiveData<List<Sura>> @Insert fun insertAll(suras: List<Sura>) @Insert fun insert(sura: Sura) @Delete fun deleteAll(suras: List<Sura>) @Delete fun delete(sura: Sura)} 这是模块 @Entity(tableName […]

类型不匹配。 必需的地图<字符串,任何>找到的地图<字符串,任何?>

我试图执行选择使用下面的anko扩展 fun read() { database.use { select(PersonTable.Name).exec { select("myTable").exec() { parseList( object : MapRowParser<Map<String, Any>> { override fun parseRow(columns: Map<String, Any?>): Map<String, Any> { Log.d("Hello", "Hello") return columns; } } ) } } } } 我收到return column上的错误 Type mismatch. Required Map<String, Any> Found Map<String, Any?> 如果我改变override fun parseRow(columns: Map<String, Any>): Map<String, Any>那么它显示一个错误。 的build.gradle apply plugin: […]

调用Rooms inMemoryBuilder方法时,Room Persistence Library运行时异常

当遵循设置Room持久性库的教程时,我在Android设备上测试时遇到此错误。 java.lang.RuntimeException:找不到PackageName .AppDatabase的实现。 AppDatabase_Impl不存在 我知道有一个类似的问题,但问题是由于kotlin gradle问题。 可能重复 测试课: @RunWith(AndroidJUnit4.class) public class LocalDatabaseTest { private PhotoDao mPhotoDao; private AppDatabase mDb; @Before public void createDb() { Context context = InstrumentationRegistry.getTargetContext(); mDb = Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class).build(); mPhotoDao = mDb.photoDao(); } @After public void closeDb() throws IOException { //mDb.close(); } @Test public void testPreConditions() { assertNotNull(mDb); } 道: @Dao public […]

请求和创建数据库从SQL转储:如何?

我试图用KOTLIN和SQLITE后端使用requery https://github.com/requery/requery库。 我有一个SQL转储,我想要在应用程序的第一次启动时写入到SQLite数据库,然后我想映射数据类到数据库实体与查询。 这里是创建表的数据源初始化: if (!(DataStorage.isDbInitialized(context))) { val db = writableDatabase val inputStream = context?.resources?.openRawResource(R.raw.dump) val reader = BufferedReader(InputStreamReader(inputStream)) val builder = StringBuilder() var line : String? var end = false while (!end) { line = reader.readLine() if(line == null) { end = true } else { builder.append(line) } } db.execSQL(builder.toString()) onCreate(db) DataStorage.setDbInitialized(context) } 我必须从SqlitexDatabaseSource和CommonDataSource派生这个类来与Kotlin一起使用。 […]

Android上的Kotlin Closable和SQLiteDatabase

我在我的项目中使用这个代码 fun getAllData(): List<Data> = writableDatabase.use { db -> db.query(…) } 它在棒棒糖设备上成功执行,但是在棒棒糖之前,它抛出一个ClassCastException异常 FATAL EXCEPTION: main java.lang.ClassCastException: android.database.sqlite.SQLiteDatabase cannot be cast to java.io.Closeable 可能是因为它是Java 1.6,并没有“试用资源”功能,但我不确定。 那么,为什么我有这个例外,如何解决呢?

我在kotlin的第一张表错误

当我尝试在Android中使用Kotlin在数据库中插入列时,我遇到了一个问题。 这是错误的: 08-04 19:45:03.781 14302-14302 / com.example.hello.note E / Zygote:v2 08-04 19:45:03.781 14302-14302 / com.example.hello.note E / Zygote:accessInfo:0 08-04 19:45:20.471 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45:20.521 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45:20.531 14302-14302 / com.example.hello.note E / Qmage:isQIO:流不是QIO文件08-04 19:45: 20.541 14302-14302 / com.example.hello.note E / MotionRecognitionManager:mSContextService = null 08-04 19:45:20.541 14302-14302 / com.example.hello.note […]

Kotlin数据库错误CursorIndexOutOfBoundsException

这是错误的,它说索引超出界限,但我不能如何解决它,有一些土耳其语的话,但他们并不重要,我认为: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.burhanozen.nothesaplama, PID: 26919 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.burhanozen.nothesaplama/com.example.burhanozen.nothesaplama.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at …….. 我试图存储某种学生信息。 我有这些错误,我在下面分享代码。 这是我的MainActivity: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) val fab = findViewById(R.id.fab) as FloatingActionButton fab.setOnClickListener { […]

类型干扰失败。 期望的类型不匹配:必需的字符串在Kotlin和Anko中找到pair <String,String>

我用kotlin和anko库创建了一个数据库。 我正在关注这篇文章https://antonioleiva.com/databases-anko-kotlin/我想在下面的数据库块中插入数据,但我得到一个错误 类型干扰失败。 期望的类型不匹配:必需的字符串找到对 fun insertPerson() { database.use { insert(PersonTable.Name, PersonTable.PersonName to "XX", PersonTable.Domain to "Technology", PersonTable.MobileNumber to "XXXXX") } } object PersonTable { val Name = "Person" val ID = "id" val PersonName = "person_name" val Domain = "domain" val MobileNumber = "mobile_number" } 的build.gradle apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' […]

DbFlow Kotlin和List <String>类型转换器

DBFlow版本:4.0.4嗨,我与ListForm转换器与dbflow Android ORM和Kotlin。 我有这样的数据类定义: @Table(database = StopsDb::class) data class FavouriteStop( @PrimaryKey @Column var id: String = "", @Index @Column var name: String = "", @Column(typeConverter = StringListConverter::class) var directions: List<String> = listOf(), @Column(typeConverter = StringListConverter::class) var selectedDirections: List<String> = listOf() ) : BaseRXModel() 因为我不想创建一个单独的表来存储字符串我创建了一个列表类型转换器是这样的: class StringListConverter : TypeConverter<String, List<String>>() { val separator = "," override […]