tl;博士:我在尝试让我的代码工作时遇到问题,现在我可以使用 Anko 创建数据库并插入列但是当我尝试插入数据并且当我在 sqliteman 上打开数据库时我的列是空的也不知道如何检索 strings/Int 中的数据以发送到View
这显然工作正常我可以看到我的数据库使用正确名称的所有列创建
class MySqlHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "db_main") {
companion object {
private var instance: MySqlHelper? = null
@Synchronized
fun getInstance(ctx: Context): MySqlHelper {
if (instance == null) {
instance = MySqlHelper(ctx.applicationContext)
}
return instance!!
}
}
override fun onCreate(db: SQLiteDatabase) {
db.createTable("db_main", true,
"_id" to INTEGER + PRIMARY_KEY + AUTOINCREMENT + NOT_NULL,
"name" to TEXT,
"day" to INTEGER)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
}
// Access property for Context
val Context.database: MySqlHelper
get() = getInstance(applicationContext)
}
这应该有效,但它没有虽然我在数据库中有所有列并且我没有收到任何错误消息,但它们没有被添加
下面的代码被调用MainActivity
val db = MySqlHelper(this)
fun addtosqllite(title: String, datepicker: DatePicker) {
db.use {
insert("db_main",
"_id" to 1,
"name" to title,
"day" to datepicker.dayOfMonth)
}
}
我不知道该怎么做, 我需要从数据库中检索数据。我一直在阅读文档,但我不知道如何实现
假设我有三列_id
,
我正在做类似的name
事情day
db.select("main_db","_id","name","day").exec {
parseSingle //something goes here to send the data to String/Int
}