swift4 sqlite3 数据绑定(sqlite3_bind

2018-08-07  本文已影响63人  姬歌

Xcode9.2
Swift version 4.0.3
sqlite3
sqlite3_bind_text

    今天搞数据库,用sqlite3_bind_text插入数据的时候总是错乱
(插入的value顺序、位置不对!且插入的数据部分乱码了!)。

let sql = "insert into UserInfo(ACCOUNT,PASSWORD,TOKEN,GETTOKENDATE) values(?,?,?,?)"
//...
let cstr =   text.cString(using: .utf8)
sqlite3_bind_text(stmt, Int32(bindIndex), cstr, -1, nil)
let sql = "insert into UserInfo(ACCOUNT,PASSWORD,TOKEN,GETTOKENDATE) 
values('\(value0)','\(value1)','\(value2)','\(value3)')"

认真检验了几个小时,网上各种查,还是错乱!一直想着是不是这句
let cstr = text.cString(using: .utf8)
导致的类型转换出问题,但swift没别的方法了啊!还很傻的试了.ascii,.unicode,更错。最后没办法了,绝望中,完全不抱希望了,再试试转OC类型

let nsStr = text as! NSString
let cstr =   nsStr.utf8String
sqlite3_bind_text(stmt, Int32(bindIndex), cstr, -1, nil)

结果,T-M-D可以了!!!!

上一篇 下一篇

猜你喜欢

热点阅读