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
testand try to insert into a table namedtestdb...sqlite3_preparefails. As a consequence,stmtwill benil. The subsequent calls fail becausestmtisnil(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).