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 our 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>
#include <sqlplus.hh> // We need to supply this header file.
#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 () { // Here we go...
Connection connection (use_exceptions); // create an object `connection`
// from the Connection class.
// Our API can handle exceptions, so let's utilize 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);
}
// an object from Query class which
// is in fact bound to connection object.
Query query = connection.query();
// 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
return -1;
}
} catch (BadQuery er) { // Print the error on the screen
cerr << "Error: " << er.error << endl;
return -1;
}
return 0;
}
//----------------------ends here----------------------
Ok, but how to compile?:
[root@pathfinder 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()
[root@pathfinder 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:
[root@pathfinder examples]# ./create_table
[root@pathfinder examples]#
We have created our database and table without any error, but let's check:
[root@pathfinder examples]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33 to server version: 3.22.32
Type 'help' for help.
mysql> show databases;
+-----------+
| Database |
+-----------+
| enderunix |
| murat |
| mysql |
| test |
+-----------+
4 rows in set (0.01 sec)
mysql> use enderunix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed mysql> describe fihrist;
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| id |int(11) | | PRI | 0 | auto_increment |
| name | text | | | NULL | |
|surname | text | | | NULL | |
| phone | text | | | NULL | |
| email | text | | | NULL | |
| web | text | | | NULL | |
+---------+---------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
mysql>
As you can see, our database and table have been created successfully. Lets write some
code to insert data to the table. |