Kotlin Android SQLite INSERT INTO 查询增量行计数,但值未插入

问题描述 投票:0回答:1

我正在尝试将一行值插入到 SQLite 数据库中。 insertorThrow 函数返回的行计数值大于 -1 并且每次都会递增。 SQLite 异常没有错误,但是当我检查表内部时,它是空的。

下面是建表查询

  database.execSQL("CREATE TABLE IF NOT EXISTS ReaderFlag ("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
            + "reader_customer_id INTEGER NOT NULL,"
            + "reader_route_id INTEGER NOT NULL,"
            + "start_end_flag TEXT NOT NULL);");

下面是函数和插入查询

    fun uploadFlagToCustomer(
    database: SQLiteDatabase,
    flags: List<StartStopPremisesFlag>
) {
    flags.forEach { action ->
        //val action = flags.get(0)
        var flagString = ""
        if (action.start) {// converting boolean to string
            flagString = "start"
        }
        if (action.stop) {// converting boolean to string
            flagString = "stop"
        }

      /*  val contentValue = ContentValues()
        contentValue.put(ReaderTable.startEndFlag, flagString)
        contentValue.put(ReaderTable.customerID, action.customer)
        contentValue.put(ReaderTable.routeID, action.route)
        contentValue.put(ReaderTable.customer_address, action.address.trim())*/
        val address = "\"${action.address.trim()}\""
        val flagUnit = "\"${flagString}\""
        System.out.println("flagvalue $flagString, ${action.customer}, ${action.route}, ${action.address}")
        val query = "INSERT INTO $Reader_Table (${ReaderTable.customerID}, ${ReaderTable.routeID}, ${ReaderTable.startEndFlag}) VALUES (${action.customer}, ${action.route}, $flagUnit)"
        val error = database.execSQL(query)
        System.out.println("errorinsert1: $error, $query")
        /*  try {
              val error:Long = database.insertOrThrow(
                  Reader_Table,
                  null,
                  contentValue
              )
              System.out.println("errorinsert1: $error")
          }catch (e: SQLiteException){
              System.out.println("errorinsert2: $e")
          }catch (er: SQLiteAbortException){
              System.out.println("errorinsert3: $er")
          }*/
    }
}

下面是数据类

data class StartStopPremisesFlag(
val customer: Int,
val route: Int,
val address: String,
var start: Boolean,
var stop: Boolean)

请帮忙,找不到丢失的东西。

android database kotlin sqlite insert
1个回答
0
投票

我发现了问题,很简单,uploadFlagToCustomer() 函数位于 ViewModel 内部,我从 AlertDialog.Builder 调用此函数。当我删除该 AlertDialog 时,它会将值插入 SQLite。

下面是Activity上的代码

    private fun callDialog(database, flags) {
    flagViewModel.uploadFlagToCustomer(database, flags)
 /*   val bl = AlertDialog.Builder(this)
    bl.setTitle("Save/Clear Flags?")
    bl.setNeutralButton("Clear", null)
    bl.setPositiveButton(
        "Save & Exit"
    ) { dialog: DialogInterface?, _: Int -> flagViewModel.uploadFlagToCustomer(database, flags) }
    val d: Dialog = bl.create()
    d.setCanceledOnTouchOutside(false)
    d.setCancelable(false)
    d.setOnDismissListener { _: DialogInterface? ->
        flagViewModel.clearFlags(routeId)
    }
    d.show()*/
}

SQlite 没有问题,是这部分导致了问题

© www.soinside.com 2019 - 2024. All rights reserved.