Swift2.3:sqlite数组错误"致命错误:索引超出范围"

时间:2016-10-23 17:09:02

标签: swift sqlite

我尝试使用sqlite来存储数据。

我只是在功能代码

之上变形
if segmentControl.selectedIndex == 0 {
        let recipe = recipes[indexPath.row]
        let aux:AnyObject = data[indexPath.row]

        let table_recipeName = aux["recipeName"]
        cell.nameLabel.text = table_recipeName as? String

        let table_recipeType = aux["recipeType"]
        cell.typeLabel.text = table_recipeType as? String

        let table_recipeImage = aux["recipeImage"]
        cell.thumbnailImageView.image = UIImage(named: table_recipeImage as! String)

        let table_recipeIsFavor = aux["recipeIsFavor"]
        cell.favorIconImageView.hidden = !recipe.isFavor

        let table_recipeUserPhoto = aux["recipeUserPhoto"]
         cell.userPhoto.image = UIImage(named: table_recipeUserPhoto as! String)

        let table_recipeUserName = aux["recipeUserName"]
        cell.userName.text = table_recipeUserName as? String


    }

并且sqlite函数在这里

 //**********************SQLite********************************//
var statement = COpaquePointer()
var data: [[String: AnyObject]] = []

func loadData() {
    let db_path = "/Users/huangpinquan/Desktop/Recipes/Recipes/Recipes.sqlite"
    var db = COpaquePointer()

    let status = sqlite3_open(db_path,&db)
    if (status == SQLITE_OK) {
        print("Open the sqlite success!\n")
    }else {
        print("Open the sqlite failed!\n")
    }

    let query_stmt = "SELECT * FROM recipe WHERE type:'麵食' "
    //let query_stmt = "SELECT * FROM recipe"
    if(sqlite3_prepare_v2(db , query_stmt, -1, &statement, nil) == SQLITE_OK) {
        //self.data.removeAllObjects()
        while (sqlite3_step(statement) == SQLITE_ROW) {
            var recipeArray: [String: AnyObject] = [:]

            let recipeName = sqlite3_column_text(statement, 0)
            let recipe_name = String.fromCString(UnsafePointer<CChar>(recipeName)) ?? ""

            let recipeType = sqlite3_column_text(statement, 1)
            //....just like "statement, 0"

            let recipeImage = sqlite3_column_text(statement, 2)
            //....

            let recipeIsFavor = sqlite3_column_int(statement, 3)
            //....

            let recipeUserPhoto = sqlite3_column_text(statement, 4)
            //....

            let recipeUserName = sqlite3_column_text(statement, 5)
            //....

            recipeArray["recipeName"] = recipe_name
            recipeArray["recipeType"] = recipe_type
            recipeArray["recipeImage"] = recipe_image
            recipeArray["recipIsFavor"] = recipe_isFavor
            recipeArray["recipeUserPhoto"] = recipe_userPhoto
            recipeArray["recipeUserName"] = recipe_userName

            data.append(recipeArray)
        }

        sqlite3_finalize(statement)
    }else {
        print("read the sqlite data failed")
    }

}

当&#34; func loadData&#34;中的代码如此

let query_stmt = "SELECT * FROM recipe WHERE type:'麵食' "

会导致错误

  

致命错误:索引超出范围

但如果这样的代码,它没有任何错误

let query_stmt = "SELECT * FROM recipe"

http://imgur.com/a/x3lr1

如果你愿意的话,这是我的整个代码。

https://gist.github.com/anonymous/79eb4b2506889929cfe92dfcab3731c4

我该怎么做才能解决此错误?

1 个答案:

答案 0 :(得分:0)

不知道你的错误在哪里,我想知道你的陈述是否汇编。我不是SQL专家,但"WHERE type:'...'"看起来很可疑。首先,您稍后调用此字段recipeType,其次,您可能需要"WHERE recipeType='string literal'",不是吗?