从sqlite表中获取数据时出现问题

时间:2019-09-26 06:31:17

标签: ios swift database sqlite

当用户在线或离线时,我正在使用可创建的应用程序,因此我使用了sqlite数据库,因此当我从表中检索数据时,我无法在这里检索数据是我的数据提取代码

func get_sub_element_offline_data(){
    self.subelementOfflineData.removeAll()
    let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
                .appendingPathComponent("EOBA.sqlite")

    if sqlite3_open(fileURL.path, &db) != SQLITE_OK {
        print("error opening database")
    }
    let queryString = "SELECT * FROM Sub_ElementList_NEW1 WHERE element_id = \(elementID)"
    var stmt:OpaquePointer?

    if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
        let errmsg = String(cString: sqlite3_errmsg(db)!)
        print("error preparing insert: \(errmsg)")
        return
    }
    while(sqlite3_step(stmt) == SQLITE_ROW){  //here while statement not able to execute
        let id = sqlite3_column_int(stmt, 0)
        let element_id = String(cString: sqlite3_column_text(stmt, 1))
        let sub_elements_id = String(cString: sqlite3_column_text(stmt, 2))
        let fk_elements_id = String(cString: sqlite3_column_text(stmt, 3))
        let title = String(cString: sqlite3_column_text(stmt, 4))
        print(id)
        print(element_id)
        print(sub_elements_id)
        print(fk_elements_id)
        print(title)
        subelementOfflineData.append(SubElementOfflineModel(id: Int(id), sub_elements_id: String(describing: sub_elements_id), title: String(describing: title)))
        do {
            let jsonData = try JSONEncoder().encode(subelementOfflineData)
            let jsonString = String(data: jsonData, encoding: .utf8)!
            print(jsonString)
        } catch {
            print(error)

        }
    }
}   

我正在基于元素列表获取数据,我检查同一表中是否存在元素ID,但仍然无法获取数据

所以请为此提供帮助

1 个答案:

答案 0 :(得分:0)

我在一个名为SundeedQLite的库中工作,它将帮助您完成所有这些工作,将其检出并以任何评论回覆