Adding / Query / Parse SQLite using Anko

tl; dr: I have problems trying to get my code to work, now I can create a database using Anko and insert columns, but when I try to insert data and when I open the database by sqliteman, my columns are empty I also don’t know how get data in lines / int to send toView

This seems to work just fine . I see that my database is created with all columns with the correct name

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)

}

This should work, but it is not . Although I have all the columns in the database, and I do not receive any error messages that they are not added.

The code below is called in 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)
     }
}

, . , , this

, _id, name day -

db.select("main_db","_id","name","day").exec {
parseSingle //something goes here to send the data to String/Int
}
+4
1

, . :

database.use {
  select("main_db", "_id", "name","day").exec {
   parseList( object : MapRowParser<Map<String, Any?>> {
    override fun parseRow(columns: Map<String, Any?>): Map<String, Any?> {
      Log.e("Your result", columns.toString())
        return columns
      }
    }
   )
 }
0

Source: https://habr.com/ru/post/1685169/


All Articles