Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //
  2. // SecondViewController.swift
  3. // shoppingList
  4. //
  5. // Created by Connor O'NEILL (001029853) on 15/11/2017.
  6. // Copyright © 2017 Connor O'NEILL (001029853). All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class SecondViewController: UIViewController {
  12.  
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. // Do any additional setup after loading the view, typically from a nib.
  16. if sqlite3_open(getDBPath(), &db) == SQLITE_OK
  17. }
  18. var db: COpaquePointer = nil
  19.  
  20. func getDBPath() ->String{
  21. let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
  22. let documentsDir = paths[0]
  23. let databasePath = (documentsDir as NSString).stringByAppendingPathComponent("shoppingListDB.db")
  24. return databasePath;
  25. }
  26. var shoppingArray:[shoppingList] = []
  27.  
  28. func selectQuery(){
  29. let selectQueryStatement = "SELECT * FROM shoppingList"
  30. var queryStatement: COpaquePointer = nil
  31. if (sqlite3_prepare_v2(db,selectQueryStatement, -1, &queryStatement, nil) == SQLITE_OK){
  32. print("Query result:")
  33. while (sqlite3_step(queryStatement) == SQLITE_ROW) {
  34. let itemField = sqlite3_column_text(queryStatement, 0)
  35. let item = String.fromCString(UnsafePointer<CChar>(itemField))!
  36. let price = sqlite3_column_double(queryStatement, 1)
  37. let groupField = sqlite3_column_text(queryStatement, 2)
  38. let group = String.fromCString(UnsafePointer<CChar>(groupField))
  39. let quantity = sqlite3_column_int(queryStatement, 3)
  40.  
  41. print("\(item)")
  42. let s = shoppingList(item: item, price: price, group: group!, quantity: quantity)
  43. shoppingArray.append(s)
  44.  
  45. }
  46. }
  47. else{
  48. print("SELECT statement no worky")
  49. }
  50. sqlite3_finalize(queryStatement)
  51. sqlite3_close(db)
  52. }
  53.  
  54.  
  55. override func didReceiveMemoryWarning() {
  56. super.didReceiveMemoryWarning()
  57. // Dispose of any resources that can be recreated.
  58. }
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement