0

I am pretty new to Xcode and especially sqlite within Xcode I am getting an error that I have no idea how to fix. I have used SQLite outside of Xcode with languages like Python but it worked differently to this.

import UIKit
import SQLite3

class ViewController: UIViewController, UITextFieldDelegate {

var db: OpaquePointer?

override func viewDidLoad() {
    super.viewDidLoad()

    let fileUrl = try!
        FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("testDB.sqlite3")

    if sqlite3_open(fileUrl.path, &db) != SQLITE_OK{
        print("Error opening database.")
        return
    }

    let createTableQuery = "CREATE TABLE IF  NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, strength TEXT)"

    if sqlite3_exec(db, createTableQuery, nil, nil, nil) != SQLITE_OK{
        print("Error creating table.")
        return
    }

    print("Database created.")
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)

}

//Properties
@IBOutlet weak var inputName: UITextField!
@IBOutlet weak var inputStrength: UITextField!

//Action
@IBAction func buttonConfirm(_ sender: Any) {
    let name = inputName.text?.trimmingCharacters(in: .whitespacesAndNewlines)
    let strength = inputStrength.text?.trimmingCharacters(in: .whitespacesAndNewlines)

    if(name?.isEmpty)!{
        print("Name is empty.")
        return;
    }

    if(strength?.isEmpty)!{
        print("Strength is empty.")
        return;
    }

    var stmt: OpaquePointer?

    let insertQuery = "INSERT INTO testDB (name, strength) VALUES(?, ?);"

    if sqlite3_prepare(db, insertQuery, -1, &stmt, nil) != SQLITE_OK{
        print("Error binding query.")
    }

    if sqlite3_bind_text(stmt, 1, name, -1, nil) != SQLITE_OK{
        print("Error binding name.")
    }

    if sqlite3_bind_text(stmt, 2, strength, -1, nil) != SQLITE_OK{
        print("Error binding strength.")
    }

    if sqlite3_step(stmt) == SQLITE_DONE{
        print("Values saved successfully.")
    }

    }

}

Most of my errors are "API called with NULL prepared statement" with none of my queries binding.

Database created.
Error binding query.
2019-11-20 09:32:18.381316+0000 PharmAssist[27925:1197718] [logging] API called with NULL prepared statement
2019-11-20 09:32:18.381453+0000 PharmAssist[27925:1197718] [logging] misuse at line 89232 of [378230ae7f]
Error binding name.
2019-11-20 09:32:18.381601+0000 PharmAssist[27925:1197718] [logging] API called with NULL prepared statement
2019-11-20 09:32:18.381719+0000 PharmAssist[27925:1197718] [logging] misuse at line 89232 of [378230ae7f]
Error binding strength.
2019-11-20 09:32:18.381835+0000 PharmAssist[27925:1197718] [logging] API called with NULL prepared statement
2019-11-20 09:32:18.381933+0000 PharmAssist[27925:1197718] [logging] misuse at line 88645 of [378230ae7f]
Message from debugger: Terminated due to signal 15
3
  • 1
    You create a table named test and try to insert into a table named testdb... Commented Nov 20, 2019 at 9:49
  • If you get an error, instead of just printing out a message and going on like nothing went wrong, you should actually handle the error in some way that makes sense. Commented Nov 20, 2019 at 9:50
  • The call to sqlite3_prepare fails. As a consequence, stmt will be nil. The subsequent calls fail because stmt is nil (they can never succeed if the prepare call fails). You must find out why the prepare call fails - it's hard to do that without further information. Also, you should change your code so that won't attemt to bind parameters to the prepared statement when the statement wasn't actually prepared (due to an error). Commented Nov 20, 2019 at 10:11

1 Answer 1

1

correct your database and table name

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.