kotlin SQLite数据库始终写入默认值,从不写入新值

时间:2018-12-25 19:49:49

标签: kotlin sqlite android-sqlite

这是我为数据库设置的类。数据库处理程序是内部类。

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.content.ContentValues
import android.util.Log
import java.sql.Date

class Scores {
    var id : Int = 0
    var dataBaseName = "ScoreDatabase"
    var averageTime = 0.0f
    val date = Date(System.currentTimeMillis()).toString()


    constructor(averageTime:Float) {
        this.averageTime = averageTime
        Log.d("Poop", averageTime.toString())
    }
    constructor()

    inner class DataBaseHandler(var context:Context, tableName:String): SQLiteOpenHelper(context, dataBaseName, null,1){

        val TABLE_NAME = tableName
        val COL_ID = "id"
        val COL_AVG = "Average_Time"
        val COL_DATE = "Date"

        override fun onCreate(db: SQLiteDatabase?) {
            val createTable = "CREATE TABLE " + TABLE_NAME +" (" +
                    COL_ID +" INTEGER PRIMARY KEY AUTOINCREMENT," +
                    COL_AVG + " VARCHAR(256)," +
                    COL_DATE +" VARCHAR(256)"

            db?.execSQL(createTable)
        }

        override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        fun insertData(score: Scores){
            val db = this.writableDatabase
            var cv = ContentValues()
            cv.put(COL_AVG,score.averageTime)
            cv.put(COL_DATE,score.date)
            var result = db.insert(TABLE_NAME,null,cv)
            if(result == -1.toLong())
                Log.d("POOP", "fail score table in addition")
            else
                Log.d("POOP", "Success score table in addition" )
        }

        fun readData(): MutableList<Scores>{
            var list: MutableList<Scores> = ArrayList()

            val db = this.readableDatabase
            val query = "Select * from $TABLE_NAME"
            val result = db.rawQuery(query,null)

            if (result.moveToFirst()){
                do {
                    var score = Scores()
                    var id = result.getString(0).toInt()
                    var AvgTime = result.getString(1).toFloat()
                    var date = result.getString(2).toString()
                    list.add(score)
                }while (result.moveToNext())
            }
            result.close()
            db.close()
            return list
        }

    }

}

我试过了,其中scores类和handler是两个单独的类,但是产生了相同的结果。

这是我写入数据库的方式(来自4个独立的活动。在每个活动中,表名均不同。在此示例中,它是“ additionDataBase”)

val scores = Scores("%.3f".format(timeKeeper.averageNumber).toFloat())
val db = scores.DataBaseHandler(context, "additionDataBase")
db.insertData(scores)

这是我从数据库中读取数据的方式,该数据库处于不同的活动中,该活动显示了每个表中的averageTime。这是其中之一的代码

val context: Context? = activity

val adb = Scores().DataBaseHandler(context!!,"additionDataBase")
val data = adb.readData()
TextViewAdScore.text = data[0].averageTime.toString() + "    " + data[1].date

我想我缺少了一些东西,但是我似乎找不到它。

到目前为止,无论我执行多少次。输出始终为0.0f

1 个答案:

答案 0 :(得分:1)

看看您在readData中做什么:

var score = Scores()
var id = result.getString(0).toInt()
var AvgTime = result.getString(1).toFloat()
var date = result.getString(2).toString()
list.add(score)

idAvgTimedate被检索但没有以任何方式使用,因此您的代码等效于仅编写list.add(Scores())。 (附带说明:没有理由让他们成为var,为什么AvgTime与其他人之间大小写不一致?)