首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode SQLClient问题

Xcode SQLClient问题
EN

Stack Overflow用户
提问于 2020-06-03 18:10:11
回答 1查看 101关注 0票数 0

我正在使用一个名为"SQLClient“的库,位于这里:https://github.com/martinrybak/SQLClient

访问位于服务器上的数据库。在安装库、荚等之后,我尝试创建一个objective头文件,并将示例代码样例放入文件中,以测试是否存在错误,但IDE给出了语法错误。

下面是我刚在我的应用程序中输入的示例代码:

代码语言:javascript
复制
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 ']'

我把评论放在我的IDE给我的错误的行旁边。

下面是一个完整的列表:

  1. 元素不是编译时间常数。
  2. 预期标识符或“(”)
  3. 预期“]”
  4. 消息发送表达式开头缺少'[‘

如有任何建议,将不胜感激

EN

回答 1

Stack Overflow用户

发布于 2020-06-03 19:17:35

下面的解决方案在Swift 5中工作。您需要将#import "SQLClient.h"添加到桥接头文件中。

podfile

代码语言:javascript
复制
use_frameworks!
platform :ios, '9.0'

install! 'cocoapods'

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

ViewController文件看起来

代码语言:javascript
复制
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 ?? "")")
    }

创建一个示例项目这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62179784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档