如何使用executeUpdate插入新行 - FMdatabase

时间:2017-04-28 17:07:02

标签: ios swift sqlite fmdb

我正在使用fmdb库。

https://github.com/ccgus/fmdb

我想插入新记录,但我不知道该怎么做:

if let db = database, let q = db.executeUpdate("insert into \(table) (catid,subcat_id,id,subcat_title,title,description,lat,lon,takhfif,images,wifi,apple_health,wc,full_time,pos,work_hours,phone,mobile,fax,website,email,address,facebook,instagram,linkedin,telegram,googleplus,twitter,publish,feature,manager,city,rating_sum,rate_count,lastip,parking,isMallID,mallID,discount_images,price_images,newProduct_images,services_images,order_online,out_upon,cat_title,cat_icon,last_modify,item_logo,cat_logo,rate_sum1,rate_sum2,rate_sum3,rate_count1,rate_count2,rate_count3,rate_title1,rate_title2,rate_title3,rate_enable) values (\(catid),\(subcat_id),\(id),'\(subcat_title)','\(title)','\(description)','\(lat)','\(lon)','\(takhfif)','\(images)',\(wifi),\(apple_health),\(wc),\(full_time),\(pos),'\(work_hours)','\(phone)','\(mobile)','\(fax)','\(website)','\(email)','\(address)','\(facebook)','\(instagram)','\(linkedin)','\(telegram)','\(googleplus)','\(twitter)',\(publish),\(feature),'\(manager)','\(city)',\(rating_sum),\(rate_count),'\(lastip)',\(parking),\(isMallID),\(mallID),'\(discount_images)','\(price_images)','\(newProduct_images)','\(services_images)',\(order_online),\(out_upon),'\(cat_title)','\(cat_icon)',\(last_modify),'\(item_logo)','\(cat_logo)',\(rate_sum1),\(rate_sum2),\(rate_sum3),\(rate_count1),\(rate_count2),\(rate_count3),'\(rate_title1)','\(rate_title2)','\(rate_title3)',\(rate_enable))") {



        }

但是在构建之前我收到了这条消息:

  

无法使用string

类型的参数列表调用executeUpdate

enter image description here

1 个答案:

答案 0 :(得分:2)

您使用了api错误。第一个参数是sql,每个值必须用"?"表示。第二个(值)是值的数组。试试这个:

if let db = database, let q = db.executeUpdate(
    "insert into \(table) (catid,subcat_id,id,subcat_title,title,description,lat,lon,takhfif,images,wifi,apple_health,wc,full_time,pos,work_hours,phone,mobile,fax,website,email,address,facebook,instagram,linkedin,telegram,googleplus,twitter,publish,feature,manager,city,rating_sum,rate_count,lastip,parking,isMallID,mallID,discount_images,price_images,newProduct_images,services_images,order_online,out_upon,cat_title,cat_icon,last_modify,item_logo,cat_logo,rate_sum1,rate_sum2,rate_sum3,rate_count1,rate_count2,rate_count3,rate_title1,rate_title2,rate_title3,rate_enable) values (?, ?, ? ...., ?, ?, ? )"
    values: [catid, subcat_id, id, subcat_title, title, description, lat, lon, takhfif, images, wifi, apple_health, wc, full_time, pos, work_hours, phone, mobile, fax, website, email, address, facebook, instagram, linkedin, telegram, googleplus, twitter, publish, feature, manager, city, rating_sum, rate_count, lastip, parking, isMallID, mallID, discount_images, price_images, newProduct_images, services_images, order_online, out_upon, cat_title, cat_icon, last_modify, item_logo, cat_logo, rate_sum1, rate_sum2, rate_sum3, rate_count1, rate_count2, rate_count3, rate_title1, rate_title2, rate_title3, rate_enable]) {

}

注意:我没有计算这些值,所以你必须确保"?"字符串内部正是您的值的计数!

也许你应该考虑一个提高代码可读性的函数。这样的事情:

extension FMDatabase {

    func insert(into table: String, row values: [(column: String, content: Any)]) {
        guard (values.count > 0) else { fatalError(values.count > 0, "Inserting nothing into a table makes no sense!") }
        var sql = "INSERT INTO \(table) ("
        var contents: [Any] = []
        for (index, value) in values.enumerated() {
            sql += index == 0 ? value.column : ", \(value.column)"
            contents.append(value.content)
        }
        sql += ") VALUES ("
        for index in 0..<values.count {
            sql += index == 0 ? "?" : ", ?"
        }
        sql += ")"
        executeUpdate(sql, values: contents)
    }

}

然后你可以插入这样的值:

if let db = database {
    db.insert(
        into: table,
        row: [
            (column: "catid", content: catid),
            (column: "subcat_id", content: subcat_id),
            ...
            (column: "rate_enable", content: rate_enable)
        ]
    )
}

它的可读性是否更好?