// Account: Create, Update, Retrieve Account *Database::createAccount(int customerId, double balance,string accountStatus, string accountType){ // create and open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); pStmt->Sql("INSERT INTO Account (customerId, balance, accountStatus, accountType) VALUES(?, ?, ?, ?);"); pStmt->BindInt(1, customerId); pStmt->BindDouble(2, balance); pStmt->BindString(3, accountStatus); pStmt->BindString(4, accountType); // executes the INSERT statement and cleans-up automatically pStmt->ExecuteAndFree(); // get the customer ID (the last row id) pStmt->Sql("SELECT last_insert_rowid();"); pStmt->Execute(); int accountId = pStmt->GetColumnInt(0); // get the int value at the zeroth column pStmt->FreeQuery(); // return the account to the calling function Database *dbObject = new Database(); //Write this transaction to the database dbObject->createTransaction(accountId, balance, "Deposit", "Time", "Opening Account Deposit"); //Build and return an account object to the calling function return new Account(accountId, customerId, balance,accountStatus, accountType); }
//Update a customer account (ie deposit, withdrawal) Account *Database::updateAccount(double balance, int accountId){ // create and open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); //Update account balance by accountId pStmt->Sql("UPDATE Account SET balance = ? where accountId = ?"); pStmt->BindDouble(1, balance); pStmt->BindInt(2, accountId); pStmt->Execute(); pStmt->FreeQuery(); //Use the getAccount function to return the updated account object return getAccount(accountId); }
Transaction *Database::createTransaction(int accountId, double amount, string tType, string tDate, string tDescript){ //Open database SQLiteDatabase *pDatabase = this->connect(); SQLiteStatement *pStmt = this->createStatement(pDatabase); //Build new transaction query pStmt->Sql("INSERT INTO Transactions1 (accountId, amount, transactionType, transactionDate, transactionDescription) VALUES(?, ?, ?, ?, ?);"); pStmt->BindInt(1, accountId); pStmt->BindDouble(2, amount); pStmt->BindString(3, tType); pStmt->BindString(4, tDate); pStmt->BindString(5, tDescript); // executes the INSERT statement and cleans-up automatically pStmt->ExecuteAndFree(); return new Transaction(); }