insertInDatabase

mail@pastecode.io avatar
unknown
swift
a year ago
820 B
18
Indexable
  func insertInDatabase() {
    NSLog("start insertInDatabase")
    var rc: Int32
    var stmt: OpaquePointer?
    let insertStatementString = "INSERT INTO Test (id, field1, field2) VALUES (1,'foo','bar')"
    let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)

    rc = sqlite3_prepare_v2(db, insertStatementString, -1, &stmt, nil)
    if rc == SQLITE_OK {
      sqlite3_bind_int(stmt, 1, 42)
      sqlite3_bind_text(stmt, 2, "foo", -1, SQLITE_TRANSIENT)
      sqlite3_bind_text(stmt, 3, "bar", -1, SQLITE_TRANSIENT)
      rc = sqlite3_step(stmt)
      if rc != SQLITE_DONE {
        NSLog("Could not insert row: \(rc)")
        return
      }
    } else {
      NSLog("\nINSERT statement is not prepared: \(rc)")
    }
    sqlite3_finalize(stmt)

    NSLog("✅ Entry inserted in Database")
  }