Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中为SqLite返回多个行或表?_Ios_Swift_Xcode_Sqlite_Uitableview - Fatal编程技术网

Ios 如何在swift中为SqLite返回多个行或表?

Ios 如何在swift中为SqLite返回多个行或表?,ios,swift,xcode,sqlite,uitableview,Ios,Swift,Xcode,Sqlite,Uitableview,我正在为swift使用SqLite库。我可以用这个方法返回一行 func getById(id:Int64) -> Row?{ do{ let query=Users.filter(Id==id) print(query.asSQL()) var data = try db!.pluck(query) return data }catch{

我正在为swift使用SqLite库。我可以用这个方法返回一行

  func getById(id:Int64) -> Row?{
        do{
            let query=Users.filter(Id==id)
            print(query.asSQL())
            var data = try db!.pluck(query)
            return data
        }catch{
            print("Error: \(error)")
            return nil
        }
    }
此函数正在返回“
行”
”正常。但我使用的是tableView对象,所以我需要一个数据源

如何返回table并为tableView设置数据源,我还没有找到这样的示例

致以最诚挚的问候。

您可以使用以下代码:

我不知道所有的类,但这是获取SQLite3查询(swift)的主要方法,检查所有结果(如果有多个),附加它们并在最后返回整个数据

//You say you want to return array of rows
func getById(id:Int64) -> [Row]? {
    //Create empty array
    var data = [Row]()
    //build the query. I use UsersTable.entityName.filter(), but I don't know your structure
    let query=Users.filter(Id==id)
    do {
        //for cycle to go to all of the results
        for rowInfo in try db!.pluck(query) {
            //Create variable. I don't know what you want. You can create it as Row() and after that add the information
            let userData = Row()
            //you need to use rowInfo, it store the SQL data. inside the key is from the table Structure. You used Users.filter, so I guess the struct with all column names is Users, so Users.name is the 'name' column
            userData.name = rowInfo[Users.name]
            //Apend the data
            data.append(userData)
        }
    } catch {
        return nil
    }
    //Return the array of Rows
    return data
}
您可以使用以下代码:

我不知道所有的类,但这是获取SQLite3查询(swift)的主要方法,检查所有结果(如果有多个),附加它们并在最后返回整个数据

//You say you want to return array of rows
func getById(id:Int64) -> [Row]? {
    //Create empty array
    var data = [Row]()
    //build the query. I use UsersTable.entityName.filter(), but I don't know your structure
    let query=Users.filter(Id==id)
    do {
        //for cycle to go to all of the results
        for rowInfo in try db!.pluck(query) {
            //Create variable. I don't know what you want. You can create it as Row() and after that add the information
            let userData = Row()
            //you need to use rowInfo, it store the SQL data. inside the key is from the table Structure. You used Users.filter, so I guess the struct with all column names is Users, so Users.name is the 'name' column
            userData.name = rowInfo[Users.name]
            //Apend the data
            data.append(userData)
        }
    } catch {
        return nil
    }
    //Return the array of Rows
    return data
}