概述
//这里是写的工具类
class wenSql(context: Context, name: String, factory: SQLiteDatabase.CursorFactory?, ver: Int) : SQLiteOpenHelper(context, name, factory, ver) {
val Create_ = "Create table wen(id integer primary key autoincrement,title text , url text);"//SQL语句
val mContext = context
var marks = arrayListOf<Map<String, Any>>()//存放数据
override fun onCreate(db: SQLiteDatabase?) {
try {
db!!.execSQL(Create_)
// Toast.makeText(mContext, "初始化完成!", Toast.LENGTH_SHORT).show()
} catch (e: NullPointerException) {
println(e)
}
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {}
fun wen_add(title: String, url: String, db: SQLiteDatabase,hand:Handler): Boolean {
try {
var cv = ContentValues()
cv.put("title", title)
cv.put("url", url)
Log.d("添加:",cv.getAsString("title"))
Log.d("添加:",cv.getAsString("url"))
db.insert("wen", null, cv)
cv.clear()
var msg = Message()
msg.what = 2
hand.sendMessage(msg)
return true
} catch (e: NullPointerException) {
var msg = Message()
msg.what = 0
hand.sendMessage(msg)//通知UI线程刷新数据
return false
}
}
fun wen_delete(sql: String): Boolean {
return false
}
fun wen_query(id: String, db: SQLiteDatabase, hand: Handler): Boolean {
var cursor = db.query("wen", null, null, null, null, null, null, null)
if (cursor.moveToFirst()) {
do {
var temMAp = linkedMapOf<String, Any>()
var title = cursor.getString(cursor.getColumnIndex("title"))
var url = cursor.getString(cursor.getColumnIndex("url"))
temMAp.put("title", title)
temMAp.put("url", url)
marks.add(temMAp)
// println(temMAp)
//temMAp.clear()
} while (cursor.moveToNext())
println(marks)
val msg = Message()
msg.what = 1
hand.sendMessage(msg)
cursor.close()
}
return false
}
fun wen_update(sql: String): Boolean {
return false
}
}
最后
以上就是寒冷冬瓜为你收集整理的Kotlin开发 SQLite使用的全部内容,希望文章能够帮你解决Kotlin开发 SQLite使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复