View difference between Paste ID: kWnknHi4 and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | // addressBook_v2.cpp: Andrew Levenson |
2 | // | |
3 | // Description: An attempt at making a simple address book using OO methods. | |
4 | // Thanks to rizlah from ##c++-basic @ Freenode for fixing the code from V1. | |
5 | ||
6 | #include <list> | |
7 | #include <string> | |
8 | #include <vector> | |
9 | #include <iostream> | |
10 | #include <algorithm> | |
11 | #include "mysqlinfo.h" | |
12 | ||
13 | /* MySQL Connector/C++ specific headers */ | |
14 | #include <cppconn/driver.h> | |
15 | #include <cppconn/connection.h> | |
16 | #include <cppconn/statement.h> | |
17 | #include <cppconn/prepared_statement.h> | |
18 | #include <cppconn/resultset.h> | |
19 | #include <cppconn/metadata.h> | |
20 | #include <cppconn/resultset_metadata.h> | |
21 | #include <cppconn/exception.h> | |
22 | #include <cppconn/warning.h> | |
23 | ||
24 | struct addressCard | |
25 | { | |
26 | std::string fName; | |
27 | std::string lName; | |
28 | std::string phone; | |
29 | std::string email; | |
30 | std::string city; | |
31 | std::string comments; | |
32 | }; | |
33 | ||
34 | class addressBook | |
35 | { | |
36 | public: | |
37 | ||
38 | void insert(addressCard, sql::Connection*); | |
39 | void remove(std::string, std::string, sql::Connection*); | |
40 | void display(sql::Connection*); | |
41 | }; | |
42 | ||
43 | addressCard | |
44 | create_card() | |
45 | { | |
46 | addressCard newCard; | |
47 | ||
48 | std::cout << "Please enter in the fields as they appear;\n\n" | |
49 | << "\tName:\t"; | |
50 | std::cin >> newCard.fName >> newCard.lName; | |
51 | std::cin.get(); | |
52 | ||
53 | std::cout << "\tPhone:\t "; | |
54 | getline(std::cin, newCard.phone); | |
55 | ||
56 | std::cout << "\teMail:\t "; | |
57 | getline(std::cin, newCard.email); | |
58 | ||
59 | std::cout << "\tCity:\t "; | |
60 | getline(std::cin, newCard.city); | |
61 | ||
62 | std::cout << "\tComments: "; | |
63 | getline(std::cin, newCard.comments); | |
64 | ||
65 | return newCard; | |
66 | } | |
67 | ||
68 | ||
69 | ||
70 | void | |
71 | display_card(addressCard const & card) | |
72 | { | |
73 | std::cout << "\nName:\t " | |
74 | << card.fName | |
75 | << " " | |
76 | << card.lName << std::endl; | |
77 | std::cout << "Phone:\t " | |
78 | << card.phone << std::endl; | |
79 | std::cout << "eMail:\t " | |
80 | << card.email << std::endl; | |
81 | std::cout << "City:\t " | |
82 | << card.city << std::endl; | |
83 | std::cout << "Comments: " | |
84 | << card.comments << std::endl; | |
85 | } | |
86 | ||
87 | void | |
88 | addressBook::insert(addressCard card, sql::Connection* con) | |
89 | { | |
90 | sql::PreparedStatement *p_stmt; | |
91 | ||
92 | // Create a prepared statement into which | |
93 | // the appropriate fields may be entered | |
94 | p_stmt = con->prepareStatement | |
95 | ("INSERT INTO address_book " | |
96 | "(first_name, last_name, phone, email, city, comments) " | |
97 | "VALUES (?,?,?,?,?,?)"); | |
98 | // Substitute the appropriate fields into the query | |
99 | p_stmt->setString(1, card.fName); | |
100 | p_stmt->setString(2, card.lName); | |
101 | p_stmt->setString(3, card.phone); | |
102 | p_stmt->setString(4, card.email); | |
103 | p_stmt->setString(5, card.city); | |
104 | p_stmt->setString(6, card.comments); | |
105 | // Update the query with the changes made, and commit. | |
106 | p_stmt->executeUpdate(); | |
107 | con->commit(); | |
108 | ||
109 | delete p_stmt; | |
110 | } | |
111 | ||
112 | void | |
113 | addressBook::remove(std::string fName, | |
114 | std::string lName, | |
115 | sql::Connection* con) | |
116 | { | |
117 | sql::PreparedStatement *p_stmt; | |
118 | ||
119 | // Create a prepared statement using the supplied | |
120 | // first and last name, to delete the indicated entry | |
121 | // from the table in the MySQL database. | |
122 | p_stmt = con->prepareStatement | |
123 | ("DELETE FROM address_book WHERE " | |
124 | "first_name = (?) AND last_name = (?)"); | |
125 | // Substitute in the appropriate fields for the DELETE statement | |
126 | p_stmt->setString(1, fName); | |
127 | p_stmt->setString(2, lName); | |
128 | // Update the query with the changes, and commit the query | |
129 | p_stmt->executeUpdate(); | |
130 | con->commit(); | |
131 | ||
132 | delete p_stmt; | |
133 | } | |
134 | ||
135 | void | |
136 | addressBook::display(sql::Connection* con) | |
137 | { | |
138 | sql::Statement *stmt; | |
139 | sql::ResultSet *res; | |
140 | ||
141 | // Create the statement object | |
142 | stmt = con->createStatement(); | |
143 | ||
144 | // Execute a query and store the result in res | |
145 | res = stmt->executeQuery("SELECT * FROM address_book " | |
146 | "ORDER BY last_name, first_name"); | |
147 | ||
148 | // Loop through the results and display them | |
149 | if(res) | |
150 | { | |
151 | while(res->next()) | |
152 | { | |
153 | std::cout << "Name: " << res->getString("first_name"); | |
154 | << " " << res->getString("last_name") << std::endl | |
155 | << "Phone: " << res->getString("phone") << std::endl | |
156 | << "eMail: " << res->getString("email") << std::endl | |
157 | << "City: " << res->getString("city") << std::endl | |
158 | << "Comments: " << res->getString("comments") | |
159 | << std::endl << std::endl; | |
160 | } | |
161 | } | |
162 | delete stmt; | |
163 | delete res; | |
164 | } | |
165 | ||
166 | void | |
167 | display_menu() | |
168 | { | |
169 | std::cout << "Would you like to:\n\n" | |
170 | << "\t1. Add a new address card\n" | |
171 | << "\t2. Delete an existing address card\n" | |
172 | << "\t3. View your address book\n" | |
173 | << "\t4. Quit\n\t::"; | |
174 | } | |
175 | ||
176 | int | |
177 | get_choice() | |
178 | { | |
179 | int choice = 0; | |
180 | while(choice > 4 || choice < 1) | |
181 | { | |
182 | if(!(std::cin >> choice)) | |
183 | { | |
184 | std::cout << "Please only enter in numbers.\n"; | |
185 | std::cin.clear(); | |
186 | std::cin.ignore(255,'\n'); | |
187 | } | |
188 | ||
189 | if(choice < 1 || choice > 4) | |
190 | { | |
191 | std::cout << "Please enter a number between 1 and 4 to " | |
192 | << "indicate your choice.\n\t::"; | |
193 | } | |
194 | } | |
195 | return choice; | |
196 | } | |
197 | ||
198 | int | |
199 | main() | |
200 | { | |
201 | sql::Driver *driver; | |
202 | sql::Connection *connection; | |
203 | /* | |
204 | sql::Statement *statement; | |
205 | sql::ResultSet *resultSet; | |
206 | sql::PreparedStatement *preparedStatement; | |
207 | sql::Savepoint *savepoint; | |
208 | */ | |
209 | ||
210 | try | |
211 | { | |
212 | driver = get_driver_instance(); | |
213 | ||
214 | // Create a DB connection using the Driver | |
215 | connection = driver->connect("localhost",USER,PASSWD); | |
216 | ||
217 | // Turn off Autocommit | |
218 | connection->setAutoCommit(0); | |
219 | ||
220 | // Select appropriate database schema | |
221 | connection->setSchema(DATABASE); | |
222 | ||
223 | } catch (sql::SQLException &e) | |
224 | { | |
225 | std::cout << "ERROR: SQLException in " << __FILE__; | |
226 | std::cout << " (" << __func__<< ") on line " | |
227 | << __LINE__ << std::endl; | |
228 | std::cout << "ERROR: " << e.what(); | |
229 | std::cout << " (MySQL error code: " << e.getErrorCode(); | |
230 | std::cout << ", SQLState: " << e.getSQLState() << ")" << std::endl; | |
231 | ||
232 | if(e.getErrorCode() == 1047) | |
233 | { | |
234 | /* | |
235 | Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR) | |
236 | Message: Unknown command | |
237 | */ | |
238 | std::cout << "\nYour server does not seem to support" | |
239 | << "Prepared Statements at all. "; | |
240 | std::cout << "Perhaps MYSQL < 4.1?" << std::endl; | |
241 | } | |
242 | ||
243 | return EXIT_FAILURE; | |
244 | ||
245 | } catch (std::runtime_error &e) | |
246 | { | |
247 | ||
248 | std::cout << "ERROR: runtime_error in " << __FILE__; | |
249 | std::cout << " (" << __func__ << ") on line " | |
250 | << __LINE__ << std::endl; | |
251 | std::cout << "ERROR: " << e.what() << std::endl; | |
252 | ||
253 | return EXIT_FAILURE; | |
254 | } | |
255 | ||
256 | std::cout << "Welcome to your Address Book!" << std::endl; | |
257 | ||
258 | addressBook * aBook = new addressBook; | |
259 | ||
260 | int choice = 0; | |
261 | while(choice != 4) | |
262 | { | |
263 | display_menu(); | |
264 | choice = get_choice(); | |
265 | ||
266 | addressCard newCard; | |
267 | std::string first, last; | |
268 | switch(choice) | |
269 | { | |
270 | case 1: | |
271 | newCard = create_card(); | |
272 | aBook->insert(newCard, connection); | |
273 | break; | |
274 | case 2: | |
275 | std::cout << "Please enter the name of the card to remove: "; | |
276 | std::cin.get(); | |
277 | std::cin >> first >> last; | |
278 | std::cin.get(); | |
279 | aBook->remove(first, last, connection); | |
280 | break; | |
281 | case 3: | |
282 | aBook->display(connection); | |
283 | break; | |
284 | } | |
285 | } | |
286 | ||
287 | connection->close(); | |
288 | delete connection; | |
289 | delete aBook; | |
290 | return 0; | |
291 | } |