Failure preparing: bad parameter or other API misuse in SQLite swift while inserting data
03:15 15 Jan 2019
let db = Database.openDatabase()

let insertStatementString = "INSERT INTO SignupDetails VALUES (\((self.txtUsername.text!.trimmingCharacters(in: .whitespaces))), \((self.txtPassword.text!.trimmingCharacters(in: .whitespaces))))"

Database.insertIntoSignupDetails(db: db, insertStatementString: insertStatementString)

sqlite3_close(db)

The above code contains my query for insert data into the database using SQLite.

I have created a table as shown as below:-

let db = Database.openDatabase()
let createTableString =
    """
        CREATE TABLE IF NOT EXISTS SignupDetails(
        username CHAR(255),
        password CHAR(255));
        """

Database.createTable(db: (db != nil) ? db : OpaquePointer(UserDefaults.standard.object(forKey: Constant.db) as! String), createTableString: createTableString)

sqlite3_close(db)

and I have also successfully executed the following code for table creation:-

static func createTable(db: OpaquePointer?, createTableString: String) {    
    var createTableStatement: OpaquePointer? = nil

    if sqlite3_prepare_v2(db, createTableString, -1, &createTableStatement, nil) == SQLITE_OK {
        if sqlite3_step(createTableStatement) == SQLITE_DONE {
            print("table created.")
        } else {
            print("table could not be created.")
        }
    } else {
        print("CREATE TABLE statement could not be prepared.")
    }

    sqlite3_finalize(createTableStatement)
}

But my problem is when I am trying to insert data into that existing table
getting the error shown as below :-

Code:-

    var insertStatement: OpaquePointer?


    guard sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK else {
        let errmsg = String(cString: sqlite3_errmsg(db))
        print("failure preparing: \(errmsg)")
        return
    }

    if sqlite3_step(insertStatement) == SQLITE_OK {
        print("Successfully inserted row.")
    } else {
        print("Could not insert row.")
    }

    sqlite3_finalize(insertStatement)

Error:-

failure preparing: bad parameter or other API misuse

ios swift sqlite