How can we connect the MySql Database with C++ Application...
Hi Guys,
If you want to develop a code in c++ and want to
connect your Mysql database with your c++ application then you can
read and use below mentioned steps. But you need to use some additional
libraries which can be downloaded from net also for the connectivity of
applications, So I am writing all the steps for that & go for that
and develop your application.
Mysql Connectivity in vc++6.0:
First we write a steps for creating a simple c++ program in Visual Studio 6.0:
First
of all we need to install Visual Studio 6.0 for the creation of c++ application.There are certain steps which
we need to follow to create the new visual studio project for c++. We just need
to follow the steps to create the c++ project in visual studio 6.0. These basic
steps are specified as follows:
STEP
1:
Firstly open the Microsoft Visual C++ 6.0. Then proceed with the next step.
STEP
2: Creating
the new Visual C++ 6.0 project. Below is the procedure for that.
STEP
2.1:
STEP
2.2:
STEP
3: For
creating the new program we need to perform following steps.
STEP 3.1:
STEP 3.2:
STEP
4: Compiling
and running the code.
STEP 4.1:
For
compiling the shortcut keys are :: ctrl + F7
For
linking shortcut key
:: F7
For
running the code shortcut keys :: ctrl + F5
STEP
4.2:
When
we run the code by (ctrl
+ F5)
the executable of the code (.exe) is automatically created in the debug
folder.
Now the Steps for Connecting Msql with C++ Application:
1.First of all install the MySQL-connector for c language mysql-connector-c-6.0.2-win32.msi.
1.First of all install the MySQL-connector for c language mysql-connector-c-6.0.2-win32.msi.
2. Include the MySQL Connector C 6.0.2 include files in vc++6.0
Tools->options->Directories include files.
3. Include the library file of
MySQL Connector C 6.0.2 in vc++6.0 Tools->options->Directories library
files.
4. Place the libmysql.dll file(which is in MySQL Connector C 6.0.2-> lib->opt ) in the
system32
Gud luck.......
Sample Code of Mysql Connectivity with C++:
#include<string.h>
#include <iostream>
#ifdef __LCC__
#include <winsock.h>
#endif
#include <windows.h>
#include <mysql.h>
#pragma comment (lib, "libmysql.lib")
using namespace std;
int main(int argc, char **argv)
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW
row;
int
num_fields;
int i;
conn =
mysql_init(NULL);
mysql_real_connect(conn, "localhost", "root",
"mysqpassword", "dbname", 0, NULL, 0);
cout<<" MYSQL Connection ";
mysql_query(conn, "show create procedure kkm_sub");
cout<<" MYSQL Connection ";
result =
mysql_store_result(conn);
num_fields =
mysql_num_fields(result);
while ((row
= mysql_fetch_row(result)))
{
for(i =
0; i < num_fields; i++)
{
cout<< row[i] <<endl;
}
cout<<"\n";
}
mysql_free_result(result);
mysql_close(conn);
}Gud luck.......
Comments
Post a Comment