Guest User

Untitled

a guest
Nov 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. func insertWordData() {
  2.  
  3. if openDatabase() {
  4.  
  5.  
  6. if let path = Bundle.main.path(forResource: "messagesToRead", ofType: "json") {
  7. var query = ""
  8. do {
  9. let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
  10.  
  11. let jsonObj = JSON(data: data)
  12. if jsonObj != JSON.null {
  13. for (_, jsonObj) in jsonObj {
  14.  
  15. let WordString = jsonObj["FIELD1"]
  16. let WordDefinition = jsonObj["FIELD2"]
  17.  
  18. query += "insert into words ((field_WordID), (field_WordString), (field_WordDefinition)) values (null, '(WordString)', '(WordDefinition)');"
  19. }
  20. }
  21. if !database.executeStatements(query) {
  22. print("Failed to insert initial data into the database.")
  23. print(database.lastError(), database.lastErrorMessage())
  24. }
  25. else {
  26. //print(words)
  27. }
  28. } catch let error {
  29. print(error.localizedDescription)
  30. }
  31. } else {
  32. print("Invalid filename/path.")
  33. }
  34.  
  35. database.close()
  36. }
  37. }
Add Comment
Please, Sign In to add comment