I am trying to implement the code below as part of a tutorial. I am using Xcode 10.2 and Swift 5. I have two lines of unresolved identifiers, marked with comments. Can someone identify the problem?
import UIKit
import SQLite3
class ViewController: UIViewController {
var db:OpaquePointer?
@IBOutlet weak var textFieldUsername: UITextField!
@IBOutlet weak var textFieldDecision: UITextField!
@IBAction func buttonSave(_ sender: Any) {
_ = textFieldUsername.text?.trimmingCharacters(in:.whitespacesAndNewlines)
_ = textFieldDecision.text?.trimmingCharacters(in:.whitespacesAndNewlines)
if (textFieldUsername.text == ""){
print ("Name is Empty")
return;
}
if (textFieldDecision.text == ""){
print ("Decision is Empty")
return;
}
var stmt: OpaquePointer?
let insertQuery = "INSERT INTO DECISIONS (username,decision) VALUES (?, ?)"
if sqlite3_prepare(db,insertQuery, -1, &stmt, nil ) != SQLITE_OK {
print("Error binding query")
}
if sqlite3_bind_text(stmt, 1, Username, -1, nil) != SQLITE_OK { // Unresolved identifier 'Username' . It is an text input field
print ("Error binding Username")
}
if sqlite3_bind_text(stmt, 2, Decision, -1, nil) != SQLITE_OK { // Unresolved Identifier 'Decision' . It is a text input field
print ("Error binding Decision")
}
if sqlite3_step(stmt) == SQLITE_DONE {
print ("Decisions saved successfully")
}
}
override func viewDidLoad() {
super.viewDidLoad()
let fileUrl = try! // try! Raise exception
FileManager.default.url(for:
.documentDirectory, // for doc. Dir.
in: .userDomainMask, // Inside of
appropriateFor: nil, // Appropriate for
create: false) // Don't create new file every time
.appendingPathComponent("decisionsDatabase.sqlite") // append file name
if sqlite3_open (fileUrl.path, &db) //open takes 2 parameters: path (defined above) and object class 'db': opaque pointer declared above
!= SQLITE_OK{
print ("Error opening Database") //Raise error if can't open database
return //stop execution
}
let createTableQuery = "CREATE TABLE IF NOT EXISTS defineDecision (id INTEGER PRIMARY KEY AUTOINCREMENT, username text, decision text)" //SQLite create table query
if sqlite3_exec (db, createTableQuery, nil,nil,nil) != SQLITE_OK{
print("Error creating table") //4 parameters for execute method:OpaquePointer,database query, +3 others. Raise exception with if
return //stop execution
}
print ("everything is fine")
}
}