我想使用blob数据类型在我的数据库中放置一个字符串数组。但是插入失败。这是我插入数据库的代码。
do {
let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
let data: NSData = try NSKeyedArchiver.archivedData(withRootObject: powerRanking, requiringSecureCoding: false) as NSData
let b = data.bytes
if sqlite3_bind_blob(stmnt, 2, b, Int32(data.count), SQLITE_TRANSIENT) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure binding name: \(errmsg)")
return
}
} catch {
print("Not done")
}
while(sqlite3_step(stmt) == SQLITE_ROW){
let id = sqlite3_column_int(stmt, 0)
let name = String(cString: sqlite3_column_text(stmt, 1))
var powerrank = sqlite3_column_blob(stmt, 2)
let blobBytesLength: Int = Int(sqlite3_column_bytes(stmt, 2))
let blobData = Data(bytes: &powerrank, count: blobBytesLength)
print(blobData)
do {
let myArrayFromDB = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(blobData) as? [String]
print(myArrayFromDB as Any)
}catch {
}
}
如果有人同时在FMDB和sqlite3中给出答案,这将很有帮助。尝试从数据库数组读取数据时为零。