Jump to content

How to Connect the c++ application to mysql server

- - - - -

  • Please log in to reply
11 replies to this topic

#1
great

great

    Newbie

  • Members
  • Pip
  • 6 posts
i have get the following code from a tutorial and compiled by a Borland c++ version 5.02 compiler .
but it give the error of unable to open the file 'BIDSDT.LIB'
and the code is as following



#include <iostream.h>
#include <winsock2.h>
#include <mysql.h>
int connections();
//using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;


int main() {
connections();
return 0;

}
int connections()
{ mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","root","123","test",0,0,0);
if (connection == NULL) {
cout << mysql_error(&mysql) << endl;}
return 1;
}



Please suggest there is any issue to compiler or Code or my method of compiling

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What compiler did the tutorial recommend you use? Borland C++ 5.02 is VERY old.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
great

great

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for to intrust !
In the tutorial author used Dev c++ compiler .But i think it is not a matter of compiler .
I had used Dev C++ also but it give building error 255
i have properly linked the required libraries.During compilation it gives no error no warning but as it going to build or make the project it gives the error ....
Please help ...

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
First of all get a decent compiler such as Code::Blocks with MinGW compiler or VC++ 2010 express. Dev-C++ is a crapy IDE that has not been updated for several years now -- its dead and buried. Then download and install MySQL++, which is c++ wrapper for the functions in mysql.h. After that read and do the MySql++ tutorial that you will find on their web site.

And you can not use that 16-bit Borland compiler to access 32-bit database engines such as MySQL. Just will not work. So you can toss out that old crapy compiler too.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
great

great

    Newbie

  • Members
  • Pip
  • 6 posts
Can any one tell me know that how i can write a simple query to get data !
i have created database with name "test" and a table in it by name "table_t".............

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
You really need to read an SQL tutorial or book. A simple query might be SELECT * FROM table; That will return everything in the table named "table". Capitalization is not important, I like to put SQL keywords in upper case. If you want to limit the query to certain field values in the table then you add a WHERE CLAUSE. For example if your table contains a field named "name" and one or more rows contains Smith then you might code it like this: SELECT * from table WHERE name = "smith"; It can get quite a bit more complicated, such as selecting from multiple tables and using multiple tables in the WHERE clause (sometimes called joins).

A good SQL tutorial should teach you all this.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
great

great

    Newbie

  • Members
  • Pip
  • 6 posts
dear i have written the Mysql queries but how i can use local variables of a function in the queries !
plz tell me

#8
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Just format the string. Here is an example in C code.
int n = 123;
char *tablename = "mytable";
char iobuf[255] = {0};
sprintf(iobuf, "SELECT * FROM %s WHERE data = %d", tablename, n);

This is a c++ example:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

using namespace std;

int main()
{
	int n = 123;
	string tablename = "mytable";
	string name = "Tom Jones";
	stringstream str;
	str << "SELECT * FROM " << tablename << " WHERE field = " << n;
	str << " AND name = '" << name << "'";
	cout << str.str() << '\n';
}

I could be a little more specific if I knew what columns are in the table you want to query.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#9
great

great

    Newbie

  • Members
  • Pip
  • 6 posts
i want to get input from the user by keyboard and serial port and then want to use it in queries .
So then how i will use string variable in the query function.

#10
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
while(true)
{
    read post #8 above.
}

Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#11
great

great

    Newbie

  • Members
  • Pip
  • 6 posts
thanx dear now my next step is to get data from serial port of a PC !

So tell the functions..........................

Edited by great, 22 April 2010 - 07:18 AM.


#12
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
If you are using MS-Windows then use win32 api functions. Read all about MS-Windows communications resources here. You will find a few example C programs, like this one.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users