Академический Документы
Профессиональный Документы
Культура Документы
Table of Contents
As we covered an introduction to MySQL last week, we'll be moving on to how we can connect to a MySQL database with C++ this week, and will move on to how we can guarantee transactional integrity in tomorrow's class. Topics that we'll cover today include:
Connecting to MySQL with Connector/C++ Exercises (2) Create your own project which connects to a database Create a database and a table, then store a limerick in the database via Connector/C++
30/11/2010
4/9/2012
Although the standard C connector will work, it presents a number of issues for us. As it's based on C (and not C++) it means we can't use strings, we can't use objects to pass data to/from the database, and generally we're hamstringing ourselves. In fact, the only positive is that connecting to a database through libmysql is that it's very simple.
30/11/2010
We get to use strings and objects, We're using a modern object-oriented API.
To use Connector/C++ with our projects, we also need to include the boost library (which is basically an extension to the STL)... ...which means our project folders will be approx. 62MB per project. And the code we use will be a little bit verbose (but not as bad as jumping through all the hoops we'd need to pass data between C and C++ objects)
30/11/2010
4/9/2012
A Server component which stores the data and handles input and output requests, and A Client component which connects to the database and provides the input commands (i.e. INSERT/UPDATE) or requests data from (i.e. SELECT) the server. This is the part that we'll be writing.
It won't come as any surprise that both halves need to be up and running for any meaningful communication to take place! So the first thing you need to do is to start your WAMP server via: Start | All Programs | wamp server | Start wamp server
30/11/2010
// Connector/C++ headers. Must be placed in our project folder. #include "cppconn/driver.h" #include "cppconn/exception.h" #include "cppconn/resultset.h" #include "cppconn/statement.h"
4/9/2012
The IP address or hostname of the database server we want to connect to, along with the port number we should connect on, A valid database server username, and A valid password for this username.
Setting up users/passwords and their permissions is outside the scope of this course - just be aware that WAMP server comes with a MySQL administration account called root which has no password.
// Specify our connection target and credentials const string server = "tcp://127.0.0.1:3306";
const string username = "root"; const string password = ""; // No password - very poor!
30/11/2010
Get a database connection driver, Connect to the database, Perform some transactions as required, and finally Disconnect from the database
We'll look at how we go about those step by step in the following slides.
30/11/2010
4/9/2012
And finally, create a SQL ResultSet object to hold the results returned from the database: sql::ResultSet *res;
30/11/2010
10
30/11/2010
4/9/2012
11
30/11/2010
12
30/11/2010
4/9/2012
13
30/11/2010
cout << "SQL error. Error message: " << e.what() << endl;
14
30/11/2010
4/9/2012
By field number (the bad way), and By field name (the good way!).
// While there are still results (i.e. rows) in our result set... while (res->next()) { // ...get each field we want and output it to the screen // Note: The first field/column in our result-set is // field 1 (one) and -NOT- field 0 (zero) cout << res->getString(1) << endl; }
15 30/11/2010
16
30/11/2010
4/9/2012
17
30/11/2010
4/9/2012
19
30/11/2010
20
30/11/2010
10
4/9/2012
21
30/11/2010
22
30/11/2010
11
4/9/2012
23
30/11/2010
Exercises
1) Create a Win32 console application that will connect to a MySQL database and display all fields from the USER_PRIVILEGES table in the information_schema database. 2) Create a second Win32 console application that will connect to a MySQL database. Include the facility for the user to enter five strings to form a limerick. Note: Use code in the following form to get a line of text from the user: string firstLine; cout << "Please enter the first line of the limerick: "; getline(cin, firstLine)
1) Modify the SQL in your second project so that it: Checks if a database called Limericks exists. If it doesn't then the database must be created. Checks if a table called UserLimericks exists. If it doesn't then the table must be created.The format of the table is just five VARCHARs
24
Inserts the user's limerick into the database, and then 30/11/2010
12
4/9/2012
Hints
The SQL command to check if a database exists and create it if not is: CREATE DATABASE IF NOT EXISTS name-of-database; The SQL command to check if a table in a database exists and create it if not is: CREATE TABLE IF NOT EXISTS name-of-table; Remember that you have to be USEing a database before you can do anything to it with SQL! The table structure for the limericks table can just be five VARCHARs called line1, line2, line3 etc.
25
30/11/2010
Example Limericks
A programming genius called Hank Wrote a system to access his bank When his memory failed him They nailed him then jailed him Now his storage is basic and dank.
A programming genius called HEAP Had trouble in getting to sleep So he made his lambs troup through a huge FOR-NEXT loop FOR 1 TO 10000: NEXT sheep.
26
30/11/2010
13