0

I search for the error, but I doesn't find the answer I understand. I'm a newby to Swift, so please be kind. I get an error [Execution was interrupted] when I try to insert a name in a database. Opening the database and creating a table is no problem.

let insertStatementString = "INSERT INTO Contact (Id, Name) VALUES (?, ?);"

func insert(name: String) {
    var insertStatement: OpaquePointer? = nil

    // 1
    if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
        let MaxIdQuery: String = "SELECT max(Id) from Contact"

        var id: Int32 = 0
        id =  Int32(query(queryStatementString: MaxIdQuery))!
        print("Id = ", id.description)

        // 2
        sqlite3_bind_int(insertStatement, 1, id)
        // 3
        sqlite3_bind_text(insertStatement, 2, name, -1, nil)

        // 4
        if sqlite3_step(insertStatement) == SQLITE_DONE {
            print("Successfully inserted row.")
        } else {
            print("Could not insert row.")
        }
    } else {
        print("INSERT statement could not be prepared.")
    }
    // 5
    sqlite3_finalize(insertStatement)
}

func query(queryStatementString: String) -> String {

    let Result: String = ""
    var queryStatement: OpaquePointer? = nil
    // 1
    if sqlite3_prepare_v2(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
        // 2
        if sqlite3_step(queryStatement) == SQLITE_ROW {
            // 3
            let id = sqlite3_column_int(queryStatement, 0)

            // 4
            let queryResultCol1 = sqlite3_column_text(queryStatement, 1)
            let name = String(cString: queryResultCol1!)

            // 5
//            print("Query Result:")
            let Result = "\(id) | \(name)"

        } else {
            let Result = "Query returned no results"
        }
    } else {
        let Result = "SELECT statement could not be prepared"
    }

    // 6
    sqlite3_finalize(queryStatement)
    print("Result:" + Result)
    return Result
}

insert(name: "Ray")  //this is where the error occurs.

//error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I found the error. Inside 'insert()' I query for the highest 'id' and that is inside 'query(). Query() is supposed to fill two variables (id and name). The query 'SELECT max(Id) from Contact" delivers only one value. So the name-variable in query() is empty which results in an error.

JosN
  • 1
  • 1
  • The error probably happens somewhere inside the `insert` function, not on the line that calls it. Can you point out the exact line? And show the complete and exact error message ([edit] your question, don't post comments). – rmaddy Jan 14 '18 at 21:03
  • Thanks maddy, I found the error (see my posting) – JosN Jan 15 '18 at 14:26
  • 1
    You should either delete your question or post a full answer below if you think it will help other readers in the future. – rmaddy Jan 15 '18 at 16:29

0 Answers0