5 Replies
      Latest reply on Apr 18, 2019 3:10 AM by OOPer
      wlionel Level 1 Level 1 (0 points)

        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")

                  

                  

                  

                }

         

        }

        • Re: Unresolved Identifiers a thorn in my Code
          OOPer Level 7 Level 7 (4,875 points)

          I cannot find any lines declaring two identifiers `Username` and `Decision`.

          I do not know how this part is organized, but you should be able to fix this sort of very basic errors.

           

          You may try something like this:

                  let username = textFieldUsername.text!
                  if sqlite3_bind_text(stmt, 1, username, -1, nil) != SQLITE_OK {
                      print ("Error binding Username")
                  }
                  let decision = textFieldDecision.text!
                  if sqlite3_bind_text(stmt, 2, decision, -1, nil) != SQLITE_OK {
                      print ("Error binding Decision")
                  }
          
          

           

          Please try to understand Swift basics and what theses codes of sample tutorials are doing. and do not copy codes from a tutorial without thinking.

            • Re: Unresolved Identifiers a thorn in my Code
              wlionel Level 1 Level 1 (0 points)

              Thanks OOper.That did it.

              • Re: Unresolved Identifiers a thorn in my Code
                wlionel Level 1 Level 1 (0 points)

                Hello:

                 

                I have an additional issue after I ran the project. I got the following messages and errors but don't understand what the mean. When I enter data into the simulator to test if input is saved to the SQLite database i get the messages below:

                 

                2019-04-17 15:29:54.051447-0400 SQLiteDB[90845:22756563] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.

                everything is fine

                2019-04-17 15:30:48.075553-0400 SQLiteDB[90845:22756563] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/wlionelwilliams/Library/Developer/CoreSimulator/Devices/336064E2-9CFC-4BBD-A52A-5A15D5CE136D/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

                2019-04-17 15:30:48.161057-0400 SQLiteDB[90845:22756563] [MC] Reading from private effective user settings.

                Error binding query

                2019-04-17 15:32:01.883869-0400 SQLiteDB[90845:22756563] [logging] API called with NULL prepared statement

                2019-04-17 15:32:01.884040-0400 SQLiteDB[90845:22756563] [logging] misuse at line 87482 of [95fbac39ba]

                Error binding Username

                2019-04-17 15:32:01.884212-0400 SQLiteDB[90845:22756563] [logging] API called with NULL prepared statement

                2019-04-17 15:32:01.884320-0400 SQLiteDB[90845:22756563] [logging] misuse at line 87482 of [95fbac39ba]

                Error binding Decision

                2019-04-17 15:32:01.884419-0400 SQLiteDB[90845:22756563] [logging] API called with NULL prepared statement

                2019-04-17 15:32:01.884474-0400 SQLiteDB[90845:22756563] [logging] misuse at line 86902 of [95fbac39ba]

                  • Re: Unresolved Identifiers a thorn in my Code
                    eskimo Apple Staff Apple Staff (11,065 points)

                    The first few items here are log noise.  However, this:

                    2019-04-17 15:32:01.883869-0400 SQLiteDB[90845:22756563] [logging] API called with NULL prepared statement

                    is significant.  SQLite is complaining that you’re calling an API with bad parameters.  Based on the print output immediately before that (Error binding query), it seems that this is one of your sqlite3_bind_xxx calls, where you’re passing NULL to the first parameter.

                    Share and Enjoy

                    Quinn “The Eskimo!”
                    Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                    let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Unresolved Identifiers a thorn in my Code
                      OOPer Level 7 Level 7 (4,875 points)

                      You should better start a new thread when you have a new question, even if it is related to the old question.

                      Once marked as SOLVED, not many readers watch the thread.

                      (Though, I'm not sure if this sort of topic "How to use SQLite3" is welcome in the forums.)

                       

                      As you see, and also referred in eskimo's comment, there's an output:

                      Error binding query

                       

                      That means this line of your code has failed:

                              if sqlite3_prepare(db, insertQuery, -1, &stmt, nil) != SQLITE_OK {
                      
                      

                       

                      The API `sqlite3_prepare` (by the way, it is not recommended to use this old API) may fail with many reasons,

                      for example, when the SQL passed is wrong.

                       

                      You need to find what is wrong with the SQL "INSERT INTO DECISIONS (username,decision) VALUES (?, ?)".

                       

                      ---

                      Showing the error message will help you what is wrong.

                              if sqlite3_prepare(db, insertQuery, -1, &stmt, nil) != SQLITE_OK {
                                  print(String(cString: sqlite3_errmsg(db))) //<- Add this line
                                  print("Error binding query")
                              }