Created
November 18, 2016 23:14
-
-
Save siavashalipour/5a4ea5f99fec634ec9d4fbdf5ac6dca8 to your computer and use it in GitHub Desktop.
End-to-end Swift Solution with IBM Bluemix Part four - 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ProfileDatabase.swift | |
// Microservice1 | |
// | |
// Created by Siavash Abbasalipour on 28/9/16. | |
// | |
// | |
import Foundation | |
import PostgreSQL | |
enum DBError: Error { | |
case fileError(String) | |
} | |
struct ProfileDatabase { | |
let dbHost = “YOUR HOST” | |
let dbName = “YOUR DB NAME” | |
let dbUsername = “YOUR USER NAME” | |
let dbPassword = “YOUR PASSWORD” | |
let dbPort = //YOUR PORT | |
init() { | |
} | |
func addToDB(fname: String, lname: String) throws -> Bool { | |
//open postgre db | |
let pgsl = PostgreSQL.PGConnection() | |
_ = pgsl.connectdb(“host=’\(dbHost)’ port=’\(dbPort)’ dbname=’\(dbName)’ user=’\(dbUsername)’ password=’\(dbPassword)’”) | |
defer { | |
pgsl.close() | |
} | |
guard pgsl.status() != .bad else { | |
throw DBError.fileError(“Internal Server Error — failed to connect to db”) | |
} | |
let query: String = “INSERT INTO account (fname, lname) VALUES (‘\(fname)’,’\(lname)’);” | |
//execute query | |
let queryResult = pgsl.exec(statement: query) | |
guard queryResult.status() == .commandOK else { | |
throw DBError.fileError(“Internal Server Error — db query error”) | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment