Introduction to Mysql++ (c++ API for
Mysql) on FreeBSD
|
||
|
||
We will try to make a brief introduction to the `complex` C++ API for Mysql on FreeBSD. We are going to explain the installation procedure and introduce the API in light of three samples... | ||
|
||
Recent version of this document can be found on ; http://www.enderunix.org/documents/mysql++.html
. -- Murat Balaban |
||
Installation for Mysql++-1.7 | ||
Let me state in the very begining that, mysql++ assumes you have mysql client libraries installed. If not, go and install them first. If you have a standart mysql-version-client.tgz/ports installation, you don't need to worry about this, they are all in place... We are going to install our API from FreeBSD ports collection:
FreeBSD will download the source for the API from one of the predefined sites, will compile and install it all for us. Installation will copy the header files to /usr/local/include ,
libsqlplus.{a|so|so.1} libraries to /usr/local/lib directory.
|
||
Sample 1: create_table.cpp | ||
Now that we have completed the
installation, we can start accessing and playing with mysql via our c++ codes. Now let's
have a look at out sample code: (You can grab this example source code along with others from http://www.enderunix.org/documents/mysql++-samples.tgz . Figure #1 ( create_table.cpp ) //------------------------------------starts here----------------------------------- #include <iostream> int main () { // Here we go... Connection connection (use_exceptions); // create an object `connection` from the Connection class. // Our API can handle exceptions, so let's utulize this try { // All the way main() is nothing but a try block connection.connect("", HOST, USERNAME, PASSWORD); // connecting.... try { // we try to select database, if some exception is returned, we'll catch it and create the database. connection.select_db(DB); // select database } catch (BadQuery er ) { // if returned an exception, catch it connection.create_db(DB); // so, no database? create it first then. connection.select_db(DB); } Query query = connection.query(); // an object from Query class which is in fact bound to connection object. // That is the query. see the overloaded << operator. We can do many things with it. query << "CREATE TABLE fihrist (id INT not null auto_increment, name TEXT not null , surname TEXT not null , phone TEXT not null , email TEXT not null , web TEXT not null , PRIMARY KEY (id), INDEX (id), UNIQUE (id))"; try { query.execute(); // execute it! } catch (BadQuery er) { // catch the exception cerr << "Error: " << er.error << endl; // Print the error on the screen return -1; } } catch (BadQuery er) { // Print the error on the screencerr << "Error: " << er.error << endl; return -1; }
return 0; } //------------------------------------------------ends here------------------------------------------------------------------------- Ok, but how to compile?: [[email protected] examples]# c++ -D_FIX_FOR_BSD_ -I/usr/local/include/mysql -L/usr/local/lib -lsqlplus create_table.cpp -o create_table /usr/local/lib/mysql/libmysqlclient.so.6: warning: tempnam() possibly used unsafely; consider using mkstemp() [[email protected] examples]# Here, via -I flag, we expand our PATH to /usr/local/include/mysql where mysql header files are located, and via -L flag, we specify the location of the library, link -lsqlplus. There comes a binary called create_table. And, when we run:
We have created our database and table without any error, but let's check:
As you can see, our database and table have been created successfully. Lets write some code to insert data to the table. |
||
Sample #2: insert_data.cpp | ||
Figure #2 ( insert_data.cpp ) //--------------------------------------------------------starts here--------------------------------------------------------------- #include <iostream> #include <sqlplus.hh> #include <string> #define HOST "localhost" // so, where's your mysql server? #define DB "enderunix" // and database name? #define USERNAME "root" // a user granted access to the above database? #define PASSWORD "" // enter the password for the above user. If there's no password, leave it as it is... int main () { struct Person { // perhaps useless, but just for future development. int id; string name; string surname; string phone; string email; string web; }; Person person; cout << "Please enter name\n"; // we get the information from the user. cin >> person.name; cout << "Please enter surname\n"; cin >> person.surname; cout << "Please enter phone number\n"; cin >> person.phone; cout << "Please enter email address\n"; cin >> person.email; cout << "Please enter web address\n"; cin >> person.web; Connection connection (use_exceptions); try { connection.connect("", HOST, USERNAME, PASSWORD); connection.select_db(DB); Query query = connection.query(); //Difference from the first sample: none but the query itself. query << "INSERT INTO fihrist " << "(id, name, surname, phone, email, web) VALUES (\"\",\"" << person.name << "\", \"" << person.surname << "\", \"" << person.phone << "\", \"" << person.email << "\", \"" << person.web << "\" )"; try { query.execute(); } catch (BadQuery er) { cerr << "Error: " << er.error << endl; return -1; } } catch (BadQuery er) { cerr << "Error: " << er.error << endl; return -1; } return 0; } //--------------------------------------------------------------ends here------------------------------------------------------------ compile:
run:
All information is successfully inserted to mysql. Lets verify:
As we can see, the data is in the table. Now, lets make some more fun, and write code to query some given information: |
||
Sample #3: select_data.cpp | ||
Figure #3 ( select_data.cpp ) //------------------------------------------------starts here----------------------------------------------------------------------------- #include <iostream> #include <sqlplus.hh> #include <iomanip> #include <string> #define HOST "localhost" // so, where's your mysql server? #define DB "enderunix" // and database name? #define USERNAME "root" // a user granted access to the above database? #define PASSWORD "" // enter the password for the above user. If there's no password, leave it as it is... int main () { struct Person { int id; string name; string surname; string phone; string email; string web; }; Person person; cout << "Please enter name\n"; // get the name to bu queried. cin >> person.name; Connection connection (use_exceptions); try { connection.connect("", HOST, USERNAME, PASSWORD); connection.select_db(DB); Query query = connection.query(); // Querymiz yapiliyor: query << "SELECT * FROM fihrist WHERE name = \"" << person.name << "\""; try { Result result = query.store(); // query.store() executes query and stores it. // and we create a result object of Result class which is bound to query object. Row row; // this is for row[""] Result::iterator i; int count = 0; for ( i = result.begin(); i != result.end() ; i++ ) { // loop till the end of result. row = *i; cout << "\nRecord #" << ++count << "\tID: " << row["id"] << endl; cout.setf(ios::left); cout << setw(10) << "Name" << row["name"] << "\n" << setw(10) << "Surname" << row["surname"] << "\n" << setw(10) << "E-Mail" << row["email"] << "\n" << setw(10) << "Phone" << row["phone"] << "\n" << setw(10) << "Web" << row["web"] << "\n"; } cout << "\nTotally, " << result.size() << " records listed.\n\n"; } catch (BadQuery er) { cerr << "Error: " << er.error << endl; return -1; } } catch (BadQuery er) { cerr << "Error: " << er.error << endl; return -1; } catch (BadConversion er) { cerr << "Error: Tried to convert \"" << er.data << "\" to a \"" << er.type_name << "\"." << endl; return -1; } return 0; } //------------------------------------------------------------------ends here------------------------------------------------------------- Compile and run:
Totally, 1 records listed. [[email protected] examples]# That's it. Good Luck:) |
||
References: | ||
Mysql Manual: http://www.mysql.com/documentation/index.html Mysql++ Manual: |