Mysql/mysql++ and C++
Hi there,
I'm getting very confused with mysql/mysql++.
I've managed to install both successfully and am trying to connect to a database on my local machine.
I've successfully created mysql user (serrix) and a database (TestDB) with a table (TestPlayer) in it.
However i've had no luck using mysql++ to compile there examples and to be honest don't completly follow the examples - it doesn't seem to specify a database, table or user/password.
Distro: Linux Gentoo
Versions: Mysql++ 2.2.2
Mysql 5.0.42
I've also tried Mysql++ 1.7.26
Any help getting either this example or your personal example working would be greatly appreciated, i'd especially be regretful for a working example which will allow me to connect to my database and lead me through the steps of connecting to a local database.
Compiling the first example code found in the documention (example below) with the following script returns this error - please note that the library's are linked:
=============== Error: ===============
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o bin/main src/main.o -L. -lmysqlclient -lmysqlpp -lmysqlclient
src/main.o: In function `main':
main.cpp:(.text+0xae): undefined reference to `connect_to_db(int, char**, mysqlpp::Connection&, char const*)'
collect2: ld returned 1 exit status
scons: *** [bin/main] Error 1
scons: building terminated because of errors.
=============== SConstruct Script: ===============
listinc=[
'include',
'/usr/include/mysql',
'/usr/include/mysql++',
]
debugcflags=['-g']
releasecflags=[]
env=Environment(CPPPATH=listinc, LIBPATH='.', CCFLAGS = releasecflags)
# list of files needed for compiling the program
main_program_list=Split("""
src/main.cpp
""")
# list of libraries needed for linking
libs_list=Split("""
mysqlclient
mysqlpp
libmysqlclient
""")
env.Program('bin/main', main_program_list, LIBS=libs_list, LIBPATH='.')
=============== Program: (main.cpp) ===============
#include <mysql++.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
// Connect to the sample database.
mysqlpp::Connection con(false);
if (!connect_to_db(argc, argv, con)) {
return 1;
}
// Retrieve a subset of the sample stock table set up by resetdb
mysqlpp::Query query = con.query();
query << "select item from stock";
mysqlpp::Result res = query.store();
// Display the result set
cout << "We have:" << endl;
if (res) {
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row = res.at(i); ++i) {
cout << '\t' << row.at(0) << endl;
}
}
else {
cerr << "Failed to get item list: " << query.error() << endl;
return 1;
}
return 0;
}
Please note that contrary to the error, the function is defined in util.h (which is linked)
=============== util.h ===============
#if !defined(MYSQLPP_UTIL_H)
#define MYSQLPP_UTIL_H
#include <mysql++.h>
extern const char* kpcSampleDatabase;
void print_stock_header(int rows);
void print_stock_row(const mysqlpp::Row& r);
void print_stock_row(const mysqlpp::sql_char& item,
mysqlpp::sql_bigint num, mysqlpp::sql_double weight,
mysqlpp::sql_double price, const mysqlpp::sql_date& date);
void print_stock_rows(mysqlpp::Result& res);
void print_stock_table(mysqlpp::Query& query);
void get_stock_table(mysqlpp::Query& query, mysqlpp::Result& res);
void print_usage(const char* program_name,
const char* extra_parms = "");
bool connect_to_db(int argc, char *argv[], mysqlpp::Connection& con,
const char* kdb = 0);
#endif // !defined(MYSQLPP_UTIL_H)