Building a Store Application With MySQL++ and C/C++
MySQL is a great cross platform database solution. One of the reasons why MySQL is so popular is because it can be accessed from so many programming languages -- including C/C++. In this article Igal shows us how to download, install and use MySQL++, which is a C library that allows us to work with MySQL databases. Igal starts with basic connection principles and works up to creating a fully interactive product store application.
Building a Store Application With MySQL++ and C/C++ - A basic program (Page 3 of 7 )
Before we begin this section, you will need to download MySQL++, which is the C++ library for interacting with MySQL databases. You can download it here and it's around 700KB. Just like MySQL, MySQL++ works on a verity of different platforms and compilers. Consult the documentation accompanying the download for the necessary information regarding your compiler. I personally use Borland's C++ Builder 5.0, but I've also worked with MySQL on Microsoft's Visual C++ 6.0, which is what I will be using throughout this article.
Once you've downloaded MySQL++, it's extremely easy to include MySQL support into your applications. Extract the zip file to c:\mysql++. Copy c:\mysql++\mysql\lib\libmySQL.dll to c:\winnt\system32 and create a new console application in Visual C++. Click the Project -> Settings menu and go to the C/C++ tab. Choose the preprocessor category and add the following line to the additional include directories text box:
Click OK. Lastly, right click on the resource files node and add c:\mysql++\lib\mysql++.lib. If you're not using Visual C++ then consult the documentation that comes with your version of MySQL++. With that said, we can begin writing a very basic program to interact with our database. Our program will connect to and then disconnect from the database.
Firstly, we need to include some header files:
Next we will create a pointer to the MySQL structure that will hold our connection. This structure is defined in the MySQL header file. What it basically does is represent a handle to a database connection. If you would like to connect to more than one database in your application then you will need to declare more than one MYSQL structure.
To initialize our MYSQL structure we must call mysql_init(). It will allocate the required memory, initialize and then return a pointer to a MYSQL object:
pConnection = mysql_init(NULL);
To be on the safe side, you might want to check that a pointer to a MYSQL object is actually returned. You can do this by checking if pConnection is null or not. Next, we actually connect to the database using mysql_real_connect(). Here's the declaration of mysql_real_connect():
mysql_real_connect MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned int client_flag)
As you can see, there's quite a lot of parameters that the mysql_read_connect function accepts. Here's a brief breakdown of what each parameters represent:
mysql: The first parameter is the MYSQL structure we've created above. It will hold the information concerning the connection once we connect to MySQL.
host: The second parameter is the location of the database server. If the database is installed on your computer locally, then you would use "localhost".
user/passwd: The third and fourth parameters are the username and password of a valid MySQL user account.
db: The fifth parameter is the database you would like to connect to. In our case this will be "Article1".
port: The sixth parameter is the port your database server will listen on. Pass 0 to indicate that the default MySQL port should be used.
unix_socket: The seventh parameter is the Unix socket number. We will pass NULL to indicate that we don't require a special Unix socket.
client_flag: The last parameter is reserved for certain flags we may want to use, for example passing CLIENT_SSL will use the SSL encryption protocol or CLIENT_COMPRESS will use a compression protocol. For a full list of flags and their descriptions, consult the MySQL++ documentation. We will pass a 0, as we do not require any of the flags.
Here's how our function call will look (note that the function will return a NULL value on an unsuccessful connection attempt):
Before we take our application to the next level, you have to know one last thing which is how to disconnect from the database. This is accomplished by a simple call to mysql_close(). The functionrequires a pointer to a MYSQL object as its only parameter. Mysql_close() will close the connection and free the memory allocated to the MYSQL structure:
Putting everything together, here's our first C++ application that uses MySQL++ to connect to and disconnect from our MySQL database, Article1:
int main(int argc, char* argv)
pConnection = mysql_init(NULL);
if(!pConnection) //error, quit the program
printf("Attempting to connect\n");
if(mysql_real_connect(pConnection,"localhost","admin","password","Article1",0,NULL,0) == NULL) //error, quit the program