Swift代码库之完整的数据库读取代码Sqlite
2019-06-25 本文已影响5人
iCloudEnd
Swift代码库之完整的数据读取代码
很多新手经常会困惑,如何存储和读取数据呢?
最佳的解决方案就是使用Sqlite。下面我就介绍一个读取数据的小例子
数据长成什么样
sqlite数据库截图读取代码
func readData(){
//Query de SELECT
let queryString = "SELECT * FROM mword limit 10"
//Statement pointer
var stmt:OpaquePointer?
//Prearando el query
if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error preparing insert: \(errmsg)")
return
}
var wordList = [FRWord]()
//Leyendo registros
while(sqlite3_step(stmt) == SQLITE_ROW){
let id = sqlite3_column_int(stmt, 0)
let mkey = String(cString: sqlite3_column_text(stmt, 1))
let tupian = String(cString: sqlite3_column_text(stmt, 2))
let bushou = String(cString: sqlite3_column_text(stmt, 3))
let guwen = String(cString: sqlite3_column_text(stmt, 4))
let pinyin = String(cString: sqlite3_column_text(stmt, 5))
let juanbie = String(cString: sqlite3_column_text(stmt, 6))
let bsbihua = sqlite3_column_int(stmt, 7)
let fbihua = sqlite3_column_int(stmt, 8)
let zbihua = sqlite3_column_int(stmt, 9)
let duyin = String(cString: sqlite3_column_text(stmt, 10))
let lizi = String(cString: sqlite3_column_text(stmt, 11))
print("\(id),\(mkey),\(pinyin),\(zbihua)")
}
}
}