How to select/insert data in mysql table using C++

  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi Pals,

I am Anes , from Kerala ,India. I am a PHP Programmer Basically
but have interest and now studying C++ , I plan to
take data from MySql in C++, for that I install mysql-connector-c++-1.1.0-win32.msi
in my Windows XP. I got a Script from net , but it will not Work... Do you
please Explain how to Insert and select data in C++ with the help of a Code
and other things related to the Driver(mysql-connector-c++-1.1.0-win32.msi).

Regards
Anes P.A :wink:
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Hi
You need to setup path to "mysql.h"(first line). As my mysql.h resides in mysql directory in include path.


Code: [ Select ]
#include <mysql/mysql.h>
#include <stdio.h>

int main(){

  MYSQL mysql;
  MYSQL_ROW row;
  MYSQL_RES *result;
 
  unsigned int num_fields;
  unsigned int i;

  mysql_init(&mysql);

  if (!mysql_real_connect(&mysql,"localhost","root","","MyDatabase",0,NULL,0))
  {
   fprintf(stderr, "Failed to connect to database: Error: %s\n",
      mysql_error(&mysql));
  }
  else {
   if(mysql_query(&mysql, "SELECT * FROM my_table"));
     //here goes the error message <!-- s:o --><img src=\"{SMILIES_PATH}/icon_surprised.gif\" alt=\":o\" title=\"Surprised\"><!-- s:o -->)
   else {
     result = mysql_store_result(&mysql);
     num_fields = mysql_num_fields(result);
     while ((row = mysql_fetch_row(result)))
     {
        unsigned long *lengths;
        lengths = mysql_fetch_lengths(result);
        for(i = 0; i < num_fields; i++)
        {
            printf("[%.*s] \t", (int) lengths[i], row[i] ? row[i] : "NULL");
        } 
        printf("\n");
     }
   }
  }

  return 0;

}
  1. #include <mysql/mysql.h>
  2. #include <stdio.h>
  3. int main(){
  4.   MYSQL mysql;
  5.   MYSQL_ROW row;
  6.   MYSQL_RES *result;
  7.  
  8.   unsigned int num_fields;
  9.   unsigned int i;
  10.   mysql_init(&mysql);
  11.   if (!mysql_real_connect(&mysql,"localhost","root","","MyDatabase",0,NULL,0))
  12.   {
  13.    fprintf(stderr, "Failed to connect to database: Error: %s\n",
  14.       mysql_error(&mysql));
  15.   }
  16.   else {
  17.    if(mysql_query(&mysql, "SELECT * FROM my_table"));
  18.      //here goes the error message <!-- s:o --><img src=\"{SMILIES_PATH}/icon_surprised.gif\" alt=\":o\" title=\"Surprised\"><!-- s:o -->)
  19.    else {
  20.      result = mysql_store_result(&mysql);
  21.      num_fields = mysql_num_fields(result);
  22.      while ((row = mysql_fetch_row(result)))
  23.      {
  24.         unsigned long *lengths;
  25.         lengths = mysql_fetch_lengths(result);
  26.         for(i = 0; i < num_fields; i++)
  27.         {
  28.             printf("[%.*s] \t", (int) lengths[i], row[i] ? row[i] : "NULL");
  29.         } 
  30.         printf("\n");
  31.      }
  32.    }
  33.   }
  34.   return 0;
  35. }


This Tutorial Will Help You http://www.cpp-home(.)com/tutorials/202_1.htm
  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi Satwant,
Thanks for your Apt Reply.Satwant I try ur code in my Dev -C++
Environment v 4.9.9.2 in Windows XP. I save file as "satwant_mysql.cpp"
in E:/C++/ Directory....

But I got some error like :

In file included from D:/Installations/wxDev-Cpp/include/mysql/mysql.h:57,
from E:\C++\satwant_mysql.cpp:1:
D:/Installations/wxDev-Cpp/include/mysql/mysql_com.h:175: error: `SOCKET' does not

name a type
D:/Installations/wxDev-Cpp/include/mysql/mysql_com.h:339: error: `SOCKET' was not

declared in this scope
D:/Installations/wxDev-Cpp/include/mysql/mysql_com.h:339: error: expected

primary-expression before "const"
D:/Installations/wxDev-Cpp/include/mysql/mysql_com.h:339: error: expected

primary-expression before "unsigned"
D:/Installations/wxDev-Cpp/include/mysql/mysql_com.h:340: error: expected

primary-expression before "unsigned"
D:/Installations/wxDev-Cpp/include/mysql/mysql_com.h:340: error: initializer

expression list treated as compound expressionExecution terminated



I Install my application(Dev-C++) in D:/Installations/wxDev-Cpp/.

How it can avoid , please Advise me.

Regards
Anes P.A :x
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

To fix this error, just include winsock.h before including mysql.h header as shown below:

#include <winsock.h>
#include <mysql/mysql.h>

And Make sure Include Path of mysql/mysql.h
set the include path in Project->Project Options->Directories->Include Directories. In "Include Directories", add the directory where packman installed the mysql headers.
And i love Daniweb :)
  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi Satwant,
According to you I include winsock.h and Include that Directory
in path D:\Installations\Dev-Cpp\include ...

But when I compile I Got Compile log as:


Compiler: Default GCC compiler
Building Makefile: "E:\C++\Makefile.win"
Finding dependencies for file: E:\C++\satwant_mysql.cpp
Executing make...
make.exe -f "E:\C++\Makefile.win" all
g++.exe -D__DEBUG__ satwant_mysql.o -o "Project1.exe"

-L"D:/Installations/wxDev-Cpp/Lib" -g3 satwant_mysql.o(.text+0x3d): In function

`main':
E:/C++/satwant_mysql.cpp:15: undefined reference to `mysql_init@4'
satwant_mysql.o(.text+0x86):E:/C++/satwant_mysql.cpp:17: undefined reference to

`mysql_real_connect@32'satwant_mysql.o(.text+0x9b):E:/C++/satwant_mysql.cpp:19:

undefined reference to `mysql_error@4'
satwant_mysql.o(.text+0xd5):E:/C++/satwant_mysql.cpp:23: undefined reference to

`mysql_query@8'
satwant_mysql.o(.text+0xef):E:/C++/satwant_mysql.cpp:26: undefined reference to

`mysql_store_result@4'
satwant_mysql.o(.text+0x106):E:/C++/satwant_mysql.cpp:27: undefined reference to

`mysql_num_fields@4'
satwant_mysql.o(.text+0x11d):E:/C++/satwant_mysql.cpp:28: undefined reference to

`mysql_fetch_row@4'
satwant_mysql.o(.text+0x141):E:/C++/satwant_mysql.cpp:31: undefined reference to

`mysql_fetch_lengths@4'
collect2: ld returned 1 exit status
make.exe: *** [Project1.exe] Error 1Execution terminated



How it can Handle, please give a Solution for this problem.

Satwant As you say i just check by change
#include <mysql/mysql.h> to #include <mysql.h> then Got Error as
no such file or directory ... So I think
#include <mysql/mysql.h> is Correct.


Regards
Anes P.A :hmm:
Attachments:
includeDirectorySnapshot.jpg

I include the Directory SnapShot

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I would highly suggest you use a different compiler. Dev-C++ hasn't been updated in over 5 years. If your running windows I'd suggest using Microsoft Visual Studio Express for C++.
  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi Spoof,
I already install Microsoft Visual Studio Express for C++ in my PC.
But I don't Know How to Develop and run a Code there ... Do you
please Give a sample code regarding MYSQL and C++ and please Tell
How to run same in Microsoft Visual Studio Express for C++ ...

thankfully
Anes P.A
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

I've read a few other threads about these issues and i've tried some of the solutions

All i found that You need to make sure that "libmysql.dll" is either in your project working directory or in the windows system directory so that Windows can find it to load it when your program is run.

At this stage Your Program is Complied successfully But Problem is "Linking Stage" where all Function declaration are linked with definitions.

Also Give Attention what "Spoof" said. :)
  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Hi Satwant,
But I don't know How to create an application in Microsoft Visual Studio 2010 Express, I work on that on trial and Error , but I cannot
Run a simple Hello World Code there!

Regards
Anes P.A
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

its ok Ahmod, Just keep trying to solve your current problem in Dev C++. Look that example link in my Previous Post. :)
  • admod
  • Newbie
  • Newbie
  • admod
  • Posts: 8

Post 3+ Months Ago

Satwant I am really Tired in that Stuff... Do you know any Book
Regarding Development of C++ using MySql...

thankfully
Anes P.A
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Hi admod

I don't know any Special book. But i can suggest you another great tutorial
http://www.devarticles(.)com/c/a/Cplusplus/Building-a-Store-Application-With-MySQL-and-C/

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 69 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.