How can I use MySQL in C++?

I am using VS2013 and I can't find anything that I can actually use in my C++ program that will work. Any help?
I can't find anything that I can actually use in my C++ program that will work. Any help?
Did you actually read? First page of results is filled with various articles and documentation.
It doesn't work. I get linker errors.
Well then post your errors, your code, and your compilation/linkage flags.
What are compilation/linkage flags?
The flags you use when invoking the compiler and linker? I'm assuming you're using an IDE.

How did you set up your IDE to link to the MySQL libraries? Can you post the code showing the includes and how you are using it?
I just went to the additional libraries and added the folder path as shown here. http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-windows-visual-studio.html
Again, post your code. Post your errors. I'm not going to ask that a dozen times.
Okay, for example, I am trying to run this code given on the page here: http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-connecting.html

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// MySQL Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	sql::mysql::MySQL_Driver *driver;
	sql::Connection *con;

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

	delete con;
	return 0;
}


Errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Error	1	error C2653: 'sql' : is not a class or namespace name	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	9	1	MySQL Test
Error	2	error C2065: 'MySQL_Driver' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	9	1	MySQL Test
Error	3	error C2065: 'driver' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	9	1	MySQL Test
Error	4	error C2653: 'sql' : is not a class or namespace name	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	10	1	MySQL Test
Error	5	error C2065: 'Connection' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	10	1	MySQL Test
Error	6	error C2065: 'con' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	10	1	MySQL Test
Error	7	error C2065: 'driver' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	12	1	MySQL Test
Error	8	error C2653: 'sql' : is not a class or namespace name	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	12	1	MySQL Test
Error	9	error C3861: 'get_mysql_driver_instance': identifier not found	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	12	1	MySQL Test
Error	10	error C2065: 'con' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	13	1	MySQL Test
Error	11	error C2065: 'driver' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	13	1	MySQL Test
Error	12	error C2227: left of '->connect' must point to class/struct/union/generic type	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	13	1	MySQL Test
Error	13	error C2065: 'con' : undeclared identifier	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	15	1	MySQL Test
Error	14	error C2541: 'delete' : cannot delete objects that are not pointers	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	15	1	MySQL Test
	15	IntelliSense: name followed by '::' must be a class or namespace name	c:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	9	2	MySQL Test
	16	IntelliSense: identifier "driver" is undefined	c:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	9	28	MySQL Test
	17	IntelliSense: name followed by '::' must be a class or namespace name	c:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	10	2	MySQL Test
	18	IntelliSense: identifier "con" is undefined	c:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	10	19	MySQL Test
	19	IntelliSense: name followed by '::' must be a class or namespace name	c:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	12	11	MySQL Test
I didn't mean linker errors before, I meant intellisense.
You're not including anything for mysql. You need to include the proper files for actually using the mysql libraries. Continue reading those documents.
No, you're not #include-ing them. They may be accessible to the linker, but it doesn't matter since you're not actually using them.
It doesn't show how to #include them in the documentation, it just shows the other portion of the code.
How am I supposed to know which files to include when it says nothing about the includes in the documentation?
I even tried the entire sample 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
software distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

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

using namespace std;

int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
   AS _message'..." << endl;

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
  /* Connect to the MySQL test database */
  con->setSchema("test");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column fata by numeric offset, 1 is the first column */
    cout << res->getString(1) << endl;
  }
  delete res;
  delete stmt;
  delete con;

} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " »
     << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}


and even the sample code given will not work.

Errors:
1
2
3
4
5
6
7
Error	8	error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?	C:\Users\Jeremy\Dropbox\C++\Projects\MySQL Test\MySQL Test\MySQL Test.cpp	84	1	MySQL Test
	9	IntelliSense: cannot open source file "boost/variant.hpp"	c:\Program Files\MySQL\Connector C++ 1.1.3\include\cppconn\connection.h	31	1	MySQL Test
	10	IntelliSense: name followed by '::' must be a class or namespace name	c:\Program Files\MySQL\Connector C++ 1.1.3\include\cppconn\connection.h	41	9	MySQL Test
	11	IntelliSense: expected an identifier	c:\Program Files\MySQL\Connector C++ 1.1.3\include\cppconn\connection.h	41	24	MySQL Test
	12	IntelliSense: identifier "ConnectPropertyVal" is undefined	c:\Program Files\MySQL\Connector C++ 1.1.3\include\cppconn\connection.h	43	35	MySQL Test
	13	IntelliSense: cannot open source file "boost/shared_ptr.hpp"	c:\Program Files\MySQL\Connector C++ 1.1.3\include\mysql_connection.h	31	1	MySQL Test
	14	IntelliSense: cannot open source file "boost/scoped_ptr.hpp"	c:\Program Files\MySQL\Connector C++ 1.1.3\include\mysql_connection.h	32	1	MySQL Test
This is almost funny.

error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?You should just turn off pre-compiled headers in your project. I don't know anything about Visual Studio 2013, so you're just going to have to work out how to do it yourself.

IntelliSense: cannot open source file "boost/variant.hpp"You appear to be using Boost indirectly. But it cannot be found by Visual Studio.

You could save yourself all this trouble if you stopped and read a bit. If you didn't realise you needed to include header files and link to a library, you have quite a bit of reading to do.
Last edited on
Topic archived. No new replies allowed.