iOS-难以将Swift字典保存到PostgreSQL数据库

时间:2018-11-30 00:54:33

标签: ios node.js swift postgresql

解决了我的问题:

尽管我正在对后端的node.js代码进行更改并将文件重新上传到服务器,但服务器并未进行更改。关闭服务器,然后使用nodemon重新运行它,现在一切都可以正确保存。


当前正在使用Swift,Node.js和PostgreSQL数据库开发iPhone应用程序。

目前,我尝试传递给数据库的所有值(除4个之外)均正确传递。我相信我已经正确定义了所有内容。在下面,我将发布我的Swift和Node.js的一些代码片段,以及保存在数据库中的值的屏幕快照。我将随时列出我的问题。

预先感谢您的帮助!

快速词典代码

let routeInfo = 
["userID": userID, 
"arrivalTime1" : arrivaltime1.text! as Any,
"arrivalTime2" : arrivaltime2.text! as Any,
"departureTime1" : "00:00", 
"departureTime2" : "00:00", 
"startPointLat": self.lat1, 
"startPointLong": self.lon1, 
"endPointLat": self.lat2, 
"endPointLong": self.lon2, 
"Driver": driver, 
"Name": self.routeName.text! as Any, 
"startAddress": startadd as Any, 
"endAddress": endadd as Any, 
"rideDate" : calendarInput.text! as Any, 
"extraTime" : "1", 
"isOneTime" : true, 
"rideDay" : calendarInput.text! as Any] as [String : Any]

按现状显示,此处列出的最后四个值未保存:“ rideDate”,“ extraTime”,“ isOneTime”,“ rideDay”

其中定义了calendarInput.text的地方:

@objc func dateChanged(datePicker: UIDatePicker) {
    let calendarFormatter = DateFormatter()
    calendarFormatter.dateFormat = "yyyy-MM-dd"
    calendarInput.text = calendarFormatter.string(from: datePicker.date)
}

在数据库中,rideDate被定义为文本数据类型,extraTime是文本数据类型,isOneTime是布尔值,rideDay是日期类型。

下一个代码段在Swift中,并且将routeInfo传递给JSONSerialization方法。

JSONSerialization

let routeURL = URL(string: "http://myServerString:3000/routes/")!
var request = URLRequest(url: routeURL)
let routeJSON = try! JSONSerialization.data(withJSONObject: routeInfo, options: .prettyPrinted)
let routeJSONInfo = NSString(data: routeJSON, encoding: String.Encoding.utf8.rawValue)! as String
request.httpBody = "routeInfo=\(routeJSONInfo)".data(using: String.Encoding.utf8)
request.httpMethod = "POST" // POST method.

这是相关的数据库插入语句:

数据库

db.any(`INSERT INTO database.\"Routes\"
(\"riderID\",
\"departureTime1\", \"departureTime2\",  
\"arrivalTime1\", \"arrivalTime2\", 
\"startPointLong\", \"startPointLat\", 
\"endPointLong\", \"endPointLat\", 
\"Name\", \"Days\", 
\"Matched\", \"Driver\", 
\"startAddress\", \"endAddress\", 
\"rideDate\", \"extraTime\", 
\"isOneTime\", \"rideDay\") 
values(
'${userID}', 
${routeJSON['arrivalTime1']}, ${routeJSON['arrivalTime2']},
${routeJSON['departureTime1']}, ${routeJSON['departureTime2']}, 
${routeJSON['startPointLong']}, ${routeJSON['startPointLat']}, 
${routeJSON['endPointLong']}, ${routeJSON['endPointLat']}, 
${routeJSON['Name']}, 0, false, false, 
${routeJSON['startAddress']}, ${routeJSON['endAddress']}, 
${routeJSON[rideDate]}, ${routeJSON['Days']}, 
${routeJSON['extraTime']}, ${routeJSON['isOneTime']},
${routeJSON['rideDay']}))`

在后端执行此插入查询后,我在服务器控制台中收到一条成功消息,提示已添加路由。

GET /routes/saved?userID=10 304 31.664 ms - -
{ extraTime: '1',
  endPointLat: 38.8976719,
  startAddress:
   'Lincoln Memorial, 2 Lincoln Memorial Cir NW, Washington, DC 20037, United States',
  arrivalTime2: '3:58 PM',
  Name: 'SO',
  arrivalTime1: '3:57 PM',
  endAddress: 'The White House, 1600 Pennsylvania Ave NW, Washington, DC 20500-0003, United States',
  isOneTime: true,
  departureTime2: '00:00',
  rideDate: '2018-11-30',
  Driver: false,
  departureTime1: '00:00',
  startPointLat: 38.8892673,
  startPointLong: -77.0501086,
  rideDay: '2018-11-30',
  userID: '10',
  endPointLong: -77.036603 }
Successfully added route.
POST /routes/ 200 63.285 ms - 52

执行完所有这些步骤之后。.所有数据但是 rideDate,rideDay,isOneTime和extraTime将被保存。它们只是在表中显示为NULL或空白值。

所有值都在必要时定义,所以我认为。

有几点想法?

  • 在创建表之后添加了数据库列,这是否会造成所有问题?
  • 我没有正确将值发送给插入调用
  • 某处缺少连接

我知道这已经很长了,并且感谢您提供的所有帮助。如有任何疑问或需要查看的其他代码,请告诉我。

谢谢!

0 个答案:

没有答案