sqlite for swift不稳定 [英] sqlite for swift is unstable

查看:99
本文介绍了sqlite for swift不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了swift项目来使用sqlite。有时,插入时实际上并没有插入正确的(或所有)值。我知道,因为我重新启动应用程序,当我回来时,条目是随机错误(没有插入的东西)或零。但有时候是正确的

I've set up swift project to use sqlite. sometimes, when inserting it doesn't actually insert the correct (or all) the values. I know because I restart the app, and when I come back in the entries are either randomly wrong (with stuff not inserted) or nil. but sometimes correct.

这里是我设置的地方,是的,插入前数据的数据是正确的。

here is where I set it, and yes the data is correct in the values before inserting.

let update = "INSERT INTO ToDoItem (itemName, completed, goalDate) " + "VALUES (?, ?, ?);"
var statement: COpaquePointer = nil
if sqlite3_prepare_v2(database, update, -1, &statement, nil) == SQLITE_OK {
    let itemName = item.itemName as String
    let completed = item.completed == true ? 1 : 0

    sqlite3_bind_text(statement, 1, itemName, -1, nil)
    sqlite3_bind_int(statement, 2, Int32(completed))

    if let goalDate = item.goalDate?.toString() {
        sqlite3_bind_text(statement, 3, goalDate, -1, nil)
    } else {
        sqlite3_bind_text(statement, 3, "", -1, nil)
    }

    //println("inserting \(itemName), \(completed) and \(item.goalDate?.toString())")
    //println("")
}

if sqlite3_step(statement) != SQLITE_DONE {
    println("error updateing table")
    sqlite3_close(database)
    return
}
sqlite3_finalize(statement)

sqlite3_close(database)

你可以在中间看到注释掉的println,如果没有注释掉,那么itemName有时会得到该字符串的一部分。

you can see the commented out println in the middle, if that is not commented out, then the itemName sometimes gets part of that string.

推荐答案

我遇到了同样的问题。我找到了解决此问题的方法。

I had the same problem. I found the way to resolve this problem.

sqlite3_bind_text(statement, 1, itemName, -1, nil) --> itemName should be UTF8 String

您应该将itemName转换为NSString并使用UTF8String将您的字符串转换为UTF8。正确的代码在这里是相同的

You should convert itemName as NSString and use UTF8String to convert your string to UTF8. Right code is the same here

let itemName = item.itemName as NSString
sqlite3_bind_text(statement, 1, itemName.UTF8String, -1, nil)

祝你好运。

这篇关于sqlite for swift不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆