CPP Beginners Tutorial to MySQL Development
Hi folks I recently have been getting flooded by requests for this tutorial. This wiki is still very young, and I do not currently have the project files for the mysql project in VC2005. I will however look for them soon. Without further wait however here is the original tutorial:
This is a working code example of CPP
working with MySQL. I have tested this code several times
over more than one machine. To make absolutely sure that it does work.
No tutorials ever gets posted on nitecon.com without it working 100%
The Source Code is documented very well so I have not added
any explinations on the code since you can read it yourself below.
Below the actual code that is displayed you will find the compiled project source for
this project so that you may just load it and run it.
Please note that it is a Visual Studio 2005 solution and I have lost my
Visual Studio 6 so I am unable to create a workspace for anything prior to VS2005.
Please keep this document as is.
You are free to use it in your source code however you want.
But please if you do use my examples here elsewhere
please post the whole document and add the linkback to the site, Thank You!.
If you found this tutorial useful and you wish to support Nitecon.com
please use our online shop, to buy yourself items :P
First things first!
Open Visual Studio, or whatever CPP Programming IDE that you normally
use.
1. Create a new Empty Console Project/Solution.
- That means it has nothing in it no pre defined headers nothing!!
2. Add a new source file to your project. Lets just call it main.cpp
3. This step is VERY IMPORTANT. If you don't follow this you will get unresolved
external errors when you try to link your app.
external errors when you try to link your app.
- Open your project settings and go to linker.
- Add addition dependency : libmysql.lib
4. Make sure you have the include files, libraries, and
binary files. If you
don't have it then just download MySQL Server without the installer.
binary files. If you
don't have it then just download MySQL Server without the installer.
- then just set the include paths for your project to those. I have included mine in global settings, because I use it so much and it's too tedious
-
to redo
project settings every single time.
Happy Coding! And as always to support us please visit our online store!
#include "my_global.h" // Include this file first to avoid problems
#include "mysql.h" // MySQL Include File
#define SERVER "localhost"
#define USER "username"
#define PASSWORD "password"
#define DATABASE "databasename"
int main()
{
MYSQL *connect; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
// This If is irrelevant and you don't need to show
it.
// I kept it in for Fault Testing.
if(!connect) // If instance didn't initialize
say so and exit with fault.
{
fprintf(stderr,"MySQL Initialization Failed");
return 1;
}
// Now we will actually connect to the specific database.
connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);
// Following if statements are unneeded too, but it's worth it to show on your
first app,
// so that if your database is empty or the query didn't return anything it
will at least
// let you know that the connection to the mysql server was established.
if(connect)
{
printf("Connection Succeeded\n");
}
else
{
printf("Connection Failed!\n");
}
MYSQL_RES *res_set; // Create a pointer to recieve the return value.
MYSQL_ROW row; // Assign variable for rows.
mysql_query(connect,"SELECT * FROM TABLE");
// Send a query to the database.
unsigned int i = 0; // Create a counter for the rows
res_set = mysql_store_result(connect); // Recieve the result and store it
in res_set
unsigned int numrows = mysql_num_rows(res_set); // Create the count to print
all rows
// This while is to print all rows and not just the first row found,
while ((row = mysql_fetch_row(res_set)) !=
NULL)
{
printf("%s\n",row[i] != NULL ?
row[i] : "NULL"); // Print
the row data
}
mysql_close(connect); // Close and shutdown
return 0;
}
There are no comments on this page. [Add comment]