MySQL with C++

Hello everybody,

Thank you for your support.

I am trying to use MySQL database with C++, but can't find any good explanations of new for me type of code.

If you know, please let me know why it doesn't work. (I wrote errors right in the code)

Or if you can send me a simple examples which does work - thanks a lot.

I also learn from these example but for some reasons they don't work:
https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-2.html

Many thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exeption.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main (void)
{

sql::mysql::MySQL_Driver *driver; //ERROR namespace "sql::mysql" does not have a member "MySQL_Driver" ERROR2 identificator is not defined.  

sql::Connection *com;
sql::Statement *stmt;

driver = sql::mysql::get_mysql_driver_instance();  // ERROR3 namespace "sql::mysql" does not have a member "get_mysql_driver_instance"

con = driver -> connect("tcp://127.0.0.1:3306","root","root");

stmt = con->createStatement();
stmt ->execute("USE " info); //ERROR4 needed a parenthesis
stmt ->execute("DROP TABLE IF EXISTS orders");
stmt ->execute("CREATE TABLE test(id INT, label CHAR(1))");

delete stmt;
delete con;
}


UPDATED CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exeption.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main (void)
{

sql::Driver *driver; 
sql::Connection *com;
sql::Statement *stmt;

driver = get_driver_instance(); 

con = driver -> connect("tcp://127.0.0.1:3306","root","root");

stmt = con->createStatement();
stmt ->execute("USE info"); 
stmt ->execute("DROP TABLE IF EXISTS orders");
stmt ->execute("CREATE TABLE test(id INT, label CHAR(1))");

delete stmt;
delete con;
}
Last edited on
Your code does not match the examples you've linked to. E.g. the examples say sql::Driver *driver;, but your code says sql::mysql::MySQL_Driver *driver;. The examples say driver = get_driver_instance();, but your code says driver = sql::mysql::get_mysql_driver_instance();.
Thank you very much Helios.
This example I took from here:
https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-query.html
But I changed it as you said and now it looks better,
Now I have only one error:

LNK2019 unauthorized external character reference _get_driver_instance in the main function.
Are you translating to English error messages in a different language?
https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2019
unresolved external symbol 'symbol' referenced in function 'function'
I say this as someone for whom English is a second language: if you can read English I strongly recommend that you configure your compiler to output error messages in English. It will make searching information on the Internet a lot easier for you.

Anyway, the error means that you need to link to the mysql library. Somewhere in the documentation should explain how to do that, but basically you should have gotten with your download a .lib file that you should add to your project's linker options.
Thank you very much Helios,
Correct, I translated in from another language.
Ok, I will try to link it again, seems that for some reason it doesn't work appropriate, because I already connected it.
Topic archived. No new replies allowed.