codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// addressBook_v2.cpp: Andrew Levenson // // Description: An attempt at making a simple address book using OO methods. // Thanks to rizlah from ##c++-basic @ Freenode for fixing the code from V1. #include <list> #include <string> #include <vector> #include <iostream> #include <algorithm> #include "mysqlinfo.h" /* MySQL Connector/C++ specific headers */ #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/statement.h> #include <cppconn/prepared_statement.h> #include <cppconn/resultset.h> #include <cppconn/metadata.h> #include <cppconn/resultset_metadata.h> #include <cppconn/exception.h> #include <cppconn/warning.h> struct addressCard { std::string fName; std::string lName; std::string phone; std::string email; std::string city; std::string comments; }; class addressBook { public: void insert(addressCard, sql::Connection*); void remove(std::string, std::string, sql::Connection*); void display(sql::Connection*); }; addressCard create_card() { addressCard newCard; std::cout << "Please enter in the fields as they appear;\n\n" << "\tName:\t"; std::cin >> newCard.fName >> newCard.lName; std::cin.get(); std::cout << "\tPhone:\t "; getline(std::cin, newCard.phone); std::cout << "\teMail:\t "; getline(std::cin, newCard.email); std::cout << "\tCity:\t "; getline(std::cin, newCard.city); std::cout << "\tComments: "; getline(std::cin, newCard.comments); return newCard; } void display_card(addressCard const & card) { std::cout << "\nName:\t " << card.fName << " " << card.lName << std::endl; std::cout << "Phone:\t " << card.phone << std::endl; std::cout << "eMail:\t " << card.email << std::endl; std::cout << "City:\t " << card.city << std::endl; std::cout << "Comments: " << card.comments << std::endl; } void addressBook::insert(addressCard card, sql::Connection* con) { sql::PreparedStatement *p_stmt; // Create a prepared statement into which // the appropriate fields may be entered p_stmt = con->prepareStatement ("INSERT INTO address_book " "(first_name, last_name, phone, email, city, comments) " "VALUES (?,?,?,?,?,?)"); // Substitute the appropriate fields into the query p_stmt->setString(1, card.fName); p_stmt->setString(2, card.lName); p_stmt->setString(3, card.phone); p_stmt->setString(4, card.email); p_stmt->setString(5, card.city); p_stmt->setString(6, card.comments); // Update the query with the changes made, and commit. p_stmt->executeUpdate(); con->commit(); delete p_stmt; } void addressBook::remove(std::string fName, std::string lName, sql::Connection* con) { sql::PreparedStatement *p_stmt; // Create a prepared statement using the supplied // first and last name, to delete the indicated entry // from the table in the MySQL database. p_stmt = con->prepareStatement ("DELETE FROM address_book WHERE " "first_name = (?) AND last_name = (?)"); // Substitute in the appropriate fields for the DELETE statement p_stmt->setString(1, fName); p_stmt->setString(2, lName); // Update the query with the changes, and commit the query p_stmt->executeUpdate(); con->commit(); delete p_stmt; } void addressBook::display(sql::Connection* con) { sql::Statement *stmt; sql::ResultSet *res; // Create the statement object stmt = con->createStatement(); // Execute a query and store the result in res res = stmt->executeQuery("SELECT * FROM address_book " "ORDER BY last_name, first_name"); // Loop through the results and display them if(res) { while(res->next()) { std::cout << "Name: " << res->getString("first_name") << " " << res->getString("last_name") << std::endl << "Phone: " << res->getString("phone") << std::endl << "eMail: " << res->getString("email") << std::endl << "City: " << res->getString("city") << std::endl << "Comments: " << res->getString("comments") << std::endl << std::endl; } } delete res; delete stmt; } void display_menu() { std::cout << "Would you like to:\n\n" << "\t1. Add a new address card\n" << "\t2. Delete an existing address card\n" << "\t3. View your address book\n" << "\t4. Quit\n\t::"; } int get_choice() { int choice = 0; while(choice > 4 || choice < 1) { if(!(std::cin >> choice)) { std::cout << "Please only enter in numbers.\n"; std::cin.clear(); std::cin.ignore(255,'\n'); } if(choice < 1 || choice > 4) { std::cout << "Please enter a number between 1 and 4 to " << "indicate your choice.\n\t::"; } } return choice; } int main() { sql::Driver *driver; sql::Connection *connection; /* sql::Statement *statement; sql::ResultSet *resultSet; sql::PreparedStatement *preparedStatement; sql::Savepoint *savepoint; */ try { driver = get_driver_instance(); // Create a DB connection using the Driver connection = driver->connect("localhost",USER,PASSWD); // Turn off Autocommit connection->setAutoCommit(0); // Select appropriate database schema connection->setSchema(DATABASE); } catch (sql::SQLException &e) { std::cout << "ERROR: SQLException in " << __FILE__; std::cout << " (" << __func__<< ") on line " << __LINE__ << std::endl; std::cout << "ERROR: " << e.what(); std::cout << " (MySQL error code: " << e.getErrorCode(); std::cout << ", SQLState: " << e.getSQLState() << ")" << std::endl; if(e.getErrorCode() == 1047) { /* Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR) Message: Unknown command */ std::cout << "\nYour server does not seem to support" << "Prepared Statements at all. "; std::cout << "Perhaps MYSQL < 4.1?" << std::endl; } return EXIT_FAILURE; } catch (std::runtime_error &e) { std::cout << "ERROR: runtime_error in " << __FILE__; std::cout << " (" << __func__ << ") on line " << __LINE__ << std::endl; std::cout << "ERROR: " << e.what() << std::endl; return EXIT_FAILURE; } std::cout << "Welcome to your Address Book!" << std::endl; addressBook * aBook = new addressBook; int choice = 0; while(choice != 4) { display_menu(); choice = get_choice(); addressCard newCard; std::string first, last; switch(choice) { case 1: newCard = create_card(); aBook->insert(newCard, connection); break; case 2: std::cout << "Please enter the name of the card to remove: "; std::cin.get(); std::cin >> first >> last; std::cin.get(); aBook->remove(first, last, connection); break; case 3: aBook->display(connection); break; } } connection->close(); delete connection; delete aBook; return 0; }
Private
[
?
]
Run code
Submit