0

Can I insert values to SQLite Database (.db) in Swift (sqlite3) without mentioning Datatype like bind_int, bind_text, etc etc. Because this is available in Android by using Content Values. Is it possible??

1
  • Yes, of course: use a high-level wrapper like GRDB.swift that hides away the low-level C interface. Commented Aug 6, 2018 at 13:13

1 Answer 1

1

I Have solved my Problem by

func BindStatement(stmt:OpaquePointer?,data:Any, pos:Int)->(Void){
    if let dt = data as? String{
        guard sqlite3_bind_text(stmt, Int32(pos), (dt as NSString).utf8String, -1, nil) == SQLITE_OK else{
            print("error binding String ",pos,data)
            return
        } 
        return
    }
    if let dt = data as? Int{
        guard sqlite3_bind_int(stmt, Int32(pos), Int32(dt)) == SQLITE_OK else{
            print("error binding Int",pos,data)
            return
        }
        return
    }
    if let dt = data as? Double{
        guard sqlite3_bind_double(stmt, Int32(pos), dt) == SQLITE_OK else{
            print("error binding Double ",pos,data)
            return
        }
    return
    }
    if let _ = data as? NSNull{
        guard sqlite3_bind_null(stmt, Int32(pos)) == SQLITE_OK else{
            print("error binding ",data)
            return
        }
        return
    }
    print("----------------nothing Binded ---------------------",pos)
}

Now can I bind key and value together? Because I saved the key-value to a Dictionary and first I have to prepare statement only with the key and then after preparing I have to bind values. So I have to iterate the Dictionary Again. Anyway??

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.