ccgus / fmdb

A Cocoa / Objective-C wrapper around SQLite
Other
13.84k stars 2.76k forks source link

how to use the update query in swift using FMDB #493

Open parvbhaskar opened 8 years ago

parvbhaskar commented 8 years ago

Hi i tried to use couples of the query format to execute the update query but every time it is giving error. Kindly share the code to update a table column with where syntex.

robertmryan commented 8 years ago

The Swift sample in the README includes an example:

let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
let fileURL = documents.URLByAppendingPathComponent("test.sqlite")

let database = FMDatabase(path: fileURL.path)

if !database.open() {
    print("Unable to open database")
    return
}

do {
    try database.executeUpdate("create table test(x text, y text, z text)", values: nil)
    try database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", values: ["a", "b", "c"])
    try database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", values: ["e", "f", "g"])

    let rs = try database.executeQuery("select x, y, z from test", values: nil)
    while rs.next() {
        let x = rs.stringForColumn("x")
        let y = rs.stringForColumn("y")
        let z = rs.stringForColumn("z")
        print("x = \(x); y = \(y); z = \(z)")
    }
} catch let error as NSError {
    print("failed: \(error.localizedDescription)")
}

database.close()

You should show us what you tried and tell us what error you received.

robertmryan commented 8 years ago

Obviously, to do UPDATE, it is

try database.executeUpdate("update test set x = ? where y = ?", values: ["foo", "bar"])
maulik6994 commented 6 years ago

Please let me help to bind the query for below example.

maulik6994 commented 6 years ago

"INSERT INTO (TABLE_TROUBLE_CODES) ((TROUBLE_CODE),(CODE_TITLE),(SERVICE_PROVIDER_TYPE),(TROUBLE_ID),(CODE_TYPE),(PLANT_CLASS_CODE)) VALUES (?,?,?,?,?,?), (obj.troubleCode!),(obj.codeTitle!), (obj.serviceProviderType!), (obj.troubleId!),(obj.codeType!),(obj.plantClassCode!)"