SHARE
TWEET

Untitled

a guest Jun 19th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func searchContactByID(_ id: Int) -> Contact? {
  2.   let queryString = "SELECT * FROM Contact WHERE ID = \(id)"
  3.  
  4.   // Execute query and save the results.
  5.   let queryResults: FMResultSet? = mainDB?.executeQuery(queryString, withArgumentsIn: nil)
  6.  
  7.   // Check if there are results.
  8.   if queryResults?.next() == true {
  9.     let contactName = queryResults!.string(forColumn: "Name")
  10.     let contactPhone = Int(queryResults!.int(forColumn: "Phone"))
  11.     let contactCity = queryResults!.string(forColumn: "City")
  12.  
  13.     return Contact(id: id, name: contactName, phoneNumber: contactPhone, city: contactCity)
  14.   }
  15.  
  16.   // No matching results.
  17.   return nil
  18. }
  19.  
  20. func searchContactByName(_ name: String) -> [Contact] {
  21.   let queryString = "SELECT * FROM Contact WHERE Name = \(name)"
  22.   let queryResults: FMResultSet? = mainDB?.executeQuery(queryString, withArgumentsIn: nil)
  23.  
  24.   // Save the results into an array.
  25.   var results = [Contact]()
  26.  
  27.   while queryResults?.next() == true {
  28.     let contactID = Int(queryResults!.int(forColumn: "ID"))
  29.     let contactPhone = Int(queryResults!.int(forColumn: "Phone"))
  30.     let contactCity = queryResults!.string(forColumn: "City")
  31.  
  32.     // Append the result to the results array.
  33.     results.append(Contact(id: contactID, name: name, phoneNumber: contactPhone, city: contactCity))
  34.   }
  35.  
  36.   return results
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top