You are on page 1of 28

step 1: required software and their links

Note: all the softwares versions are tested, opensource and


reliable

install all these software on your computer system

 codeBlocks-16.01-setup
 devcpp_v4_9_9_2_setup
 xampp-win32-7.0.3-0-VC14-installer

Step 2 : create new project in code blocks


 open code blocks
 select create new project
 Select Console app and click on "go" button
 click next
 select C++ and click next
 Give your project name
 create your workspace (folder to save project in any drive
normally C: ) rest of things are automatically given by code
block
 click on next
 click on Finish
 This home screen will appear after you will expand source
and click on main.cpp
step 3: build and run your project once you
will get
in your codeBlocks work space folder your will
get below folder structure
 I am highlighting bin folder and .exe created
of your project name
step 4: add below mentioned header files to
"C:\Dev-Cpp\include\" sometimes they are missing so
copy them explicitly

1. my_alloc.h :
2. my_list.h :
3. mysql.h
4. mysql_com.h
5. mysql_time.h
6. mysql_version.h
7. typelib.h

step 5: add libmysql.a file from below link to


"C:\Dev-Cpp\lib\"
step 6: codeBlock project configuration time
Click on "No" when ask for relative path
Again say no to relative path
Configuration is Done Here!
 Again Build and Run your project
 Note: if there is any build error again try for configuration
setting until it succeed.
step 7: create one database example: "mca"
and table example: "mcafy" using XAMPP-
>phpMyAdmin
Note: use XAMPP as we are using it for IWT
subject, please do not install/run any other
mysql server because it can create port
problem
step 8: time to write code
add this code to your projects "main.cpp" and
use this same code to know config is working
#include <iostream>
#include <windows.h>
#include <mysql.h>

using namespace std;

int main()
{
MYSQL* conn;
conn = mysql_init(0);

conn = mysql_real_connect(conn,"localhost","root","root","mca",0,NULL,0);

if(conn)
cout<<"connection to mca databse successful "<<endl;
else
cout<<"connection problem: "<<mysql_error(conn)<<endl;

cout << "Hello world!" << endl;


return 0;
}

step 9: Build and Run your project you will get


this error due to lack of libmysql.dll file
put " libmysql.dll" file in bin folder of your
project present in codeBlock workspace
link for libmysql.dll
in my case it is : " C:\codeblockWS\MyC++SQLApp\bin\Debug"
step 10: build and run your project again and it
will successfully run now
Step 11: Some more code to try for
this contains select and insert query for user
given values
include <iostream>
#include <string>
#include <windows.h>
#include <mysql.h>

using namespace std;

int main()
{
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES *res;
int qstate;

conn = mysql_init(0);
if(conn)
cout<<"connection object ok, conn="<<conn<<endl;
else
cout<<"conn object problem: "<<mysql_error(conn);

conn = mysql_real_connect(conn,"localhost","root","","mca",0,NULL,0);

if(conn)
{
cout<<"connected to database mca"<<endl;

string id,name,phone;
cout<<"enter id: "<<endl; cin>>id;
cout<<"enter name: "<<endl; cin>>name;
cout<<"enter phone: "<<endl; cin>>phone;

string query="insert into mcafy(id,name,phone) values('"+id+"','"+name+"','"+phone+"')";

const char* q = query.c_str();

cout<<"query is: "<<q<<endl;


qstate = mysql_query(conn,q);

if(!qstate)
cout<<"record inserted successfully..."<<endl;
else
cout<<"query problem: "<<mysql_error(conn)<<endl;

qstate = mysql_query(conn,"select * from mcafy");

if(!qstate)
{
res = mysql_store_result(conn);
while(row=mysql_fetch_row(res))
{
cout<<"id: "<<row[0]<< " "
<<"name: "<<row[1]<< " "
<<"phone: "<<row[2]<<endl;
}
}
else
{
cout<<"query error: "<<mysql_error(conn)<<endl;
}
}
else
{
cout<<"connection problem: "<<mysql_error(conn)<<endl;
}

mysql_close(conn);

return 0;
}

output:

You might also like