Click to See Complete Forum and Search --> : c++ in collaboration with mysql


PsSheba
December 6th, 2004, 08:24 AM
Does any one know how to contact a mysql server through a c++ application ?
I'd like to run a code such as:

int main()
// this is a mysql command
use MyDb;
for(int i=0; i++; i<10)
// The following is a mysql command
INSERT INTO MyTable(Counter)
VALUES(i);
retrun (0);

can anyone guide me how to write the mysql commands using c++ code ?
Thanks/

Luchin_plusplus
December 6th, 2004, 08:56 AM
There is a library called mysql++, which implements the functionality you're asking. It can even work your queries as if they were iostreams:


mysqlstream myquery;
myquery << "SELECT * FROM TABLE1"<< endl;
myquery.parse(); // executes the query and restores info in a "Record" object or something like that


Results are obtained in a special kind of object from which you can select the row as an array index, and the attribute as a map index:

mysqlresult myresult= myquery.result();
Row e1= result[1];
cout<< "First person's age: "<< e1["age"]<< endl;


At least, I think it works thus way.

The problem is, the current version is very C++-inflexible and thus won't work with compilers such as VC++ 6.0 which "poorly implements C++" (according to the author's website). You will need to either
a) DL another compiler (DevC++ for example)
b) use a newer version of Visual C++ (maybe the 2003 Toolkit)
c) DL an older version of mysql++, which is not guaranteed to work all the way you could expect.

Anyway I suggest you to try, I am doing that too!


I don't know the website, but you should Google it with the "lucky" button and you should get there immediately.

PsSheba
December 6th, 2004, 10:24 AM
I'll try it.