经过几个小时的搜索堆栈并查看类似的问题后,我仍然无法使用它。请帮助指导我!
我要做的就是向我的服务器提交用户名并将其保存在MySQL中 - 只是为了让它正常工作......
我可以将用户名发送到服务器,甚至将硬编码数据返回给iOS模拟器设置标签的文本但是一旦我添加了将数据保存到MySQL的功能,我就会收到错误:“错误域= NSCocoaErrorDomain代码= 3840”字符0周围的值无效。“UserInfo = {NSDebugDescription =字符0周围的值无效。}”
很抱歉奇怪的命名惯例,我偏离了我关注的教程。谢谢!
SWIFT 3:
func Apitest(name: String) {
let json = ["user" : name]
do {
let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let url = NSURL(string: "I put the actual path here, don't worry")!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in
if error != nil{
print("First Error -> \(error)")
return
}
do {
let result = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print("Result -> \(result!)")
let label = result?["messages"] as! String
print(label)
self.setupLabel(text: label)
} catch {
print("Second Error -> \(error)")
}
}
task.resume()
} catch {
print(error)
}
}
API代码(python / flask)
@app.route('/api/get_messages/') methods=['POST'])
def get_messages():
json = request.get_json()
api_test(json['user'])
return jsonify({'messages':json['user']})
MySQL功能:
def api_test(user):
c, conn = connection()
c.execute("INSERT INTO users (email) VALUES (%s)", (user))
conn.commit()
conn.close()
c.close()
答案 0 :(得分:0)
从查看代码我会说你的插入命令应该是:
c.execute("INSERT INTO users (email) VALUES (%s)", (user))