0

I am using a library called "SQLClient", located here: https://github.com/martinrybak/SQLClient

to access my database which is located on a server. After installing the library, the pods, etc, I tried making an objective-c header file and put the example code sample into the file to test if there were no errors, but the IDE is giving me syntax errors.

Here's the example code I just put in my app:

SQLClient* client = [SQLClient sharedInstance]; //initializer element is not a compile time constant

[client connect:@"server\instance:port" username:@"user" password:@"pass" database:@"db" completion:^(BOOL success) { //expected identifier or '('
    if (success) {
      [client execute:@"SELECT * FROM Users" completion:^(NSArray* results) {
        for (NSArray* table in results) {
          for (NSDictionary* row in table) {
            for (NSString* column in row) {
              NSLog(@"%@=%@", column, row[column]);
            }
          }
        }
        [client disconnect];
      }];
    }
}]; // expected ']'

I put comments next to the lines with the errors my IDE was giving me.

here's a full list:

  1. initializer element is not a compile time constant
  2. expected identifier or '('
  3. expected ']'
  4. missing '[' at start of message send expression

Any suggestions would be appreciated

izzykk
  • 31
  • 6

1 Answers1

0

Below solution works in Swift 5 . You need to add the #import "SQLClient.h" to the bridging header file.

podfile

use_frameworks!
platform :ios, '9.0'

install! 'cocoapods'

target 'SQLClient' do
use_frameworks!
pod 'SQLClient', '~> 1.0.0'
end

ViewController file looks like,

override func viewDidLoad() {
        super.viewDidLoad()
         //Adding the observer for error and to receive the message
        NotificationCenter.default.addObserver(self, selector: #selector(error(_:)), name: NSNotification.Name.SQLClientError, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(message(_:)), name: NSNotification.Name.SQLClientMessage, object: nil)

        let client = SQLClient.sharedInstance()!
                  client.connect("ServerNameOrIP", username: "cool", password: "cool", database: "database") { success in
                  client.execute("SELECT * FROM table", completion: { (_ results: ([Any]?)) in
                   for table in results as! [[[String:AnyObject]]] {
                       for row in table {
                           for (columnName, value) in row {
                               print("\(columnName) = \(value)")
                           }
                       }
                   }
                   client.disconnect()
               })
           }
          }

    @objc func error(_ notification: Notification?) {
     let code = notification?.userInfo?[SQLClientCodeKey] as? NSNumber
     let message = notification?.userInfo?[SQLClientMessageKey] as? String
     let severity = notification?.userInfo?[SQLClientSeverityKey] as? NSNumber
     if let code = code, let severity = severity {
         print("Error #\(code): \(message ?? "") (Severity \(severity))")
     }
 }

    @objc func message(_ notification: Notification?) {
        let message = notification?.userInfo?[SQLClientMessageKey] as? String
        print("Message: \(message ?? "")")
    }

Created a sample project here