Hello, I'm trying to get Mysql working with codeblocks. I've linked the libraries (libmysql.lib an d mysqlclient.lib) an set the search directory to the mysql include folder.
It is actually linking the libraries (if I change the name I get an error), but when compiling I get errors about undefined references to mysql_init, mysql_query etc.
This is the code:
#include <iostream>
#include "mysql.h"
using namespace std;
MYSQL_RES* Perform_Query(MYSQL* connection, const char* query)
{
// send the query to the database
if (mysql_query(connection, query))
{
cout << "MySQL query error : %s\n" << mysql_error(connection) << endl;
exit(1);
}
return mysql_use_result(connection);
}
int main()
{
string mainServer="127.0.0.1";
string mainDbUser="root";
string mainDbPass="warmoes";
MYSQL *connect; //database connection variable
connect=mysql_init(NULL);
if(!connect)
cout<<"Couldn't initiate connector\n";
if (mysql_real_connect(connect, mainServer.c_str(), mainDbUser.c_str(), mainDbPass.c_str(), "test" ,0,NULL,0))
{
cout<<"Connection done\n";
MYSQL_RES* res = Perform_Query(connect, "show tables");
cout << "Tables in database test" << endl;
MYSQL_ROW row;
while ((row = mysql_fetch_row(res)) != NULL)
{
cout << row[0] << endl;
}
mysql_free_result(res);
}
else
{
cout<<mysql_error(connect)<<endl;
}
mysql_close (connect);
return 0;
}
Any clues on what's going wrong here? I work on Windows 7.