我正在使用一个名为"SQLClient“的库,位于这里:https://github.com/martinrybak/SQLClient
访问位于服务器上的数据库。在安装库、荚等之后,我尝试创建一个objective头文件,并将示例代码样例放入文件中,以测试是否存在错误,但IDE给出了语法错误。
下面是我刚在我的应用程序中输入的示例代码:
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给我的错误的行旁边。
下面是一个完整的列表:
如有任何建议,将不胜感激
发布于 2020-06-03 19:17:35
下面的解决方案在Swift 5中工作。您需要将#import "SQLClient.h"添加到桥接头文件中。
podfile
use_frameworks!
platform :ios, '9.0'
install! 'cocoapods'
target 'SQLClient' do
use_frameworks!
pod 'SQLClient', '~> 1.0.0'
endViewController文件看起来
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 ?? "")")
}创建一个示例项目这里
https://stackoverflow.com/questions/62179784
复制相似问题