// 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
You're not including anything for mysql. You need to include the proper files for actually using the mysql libraries. Continue reading those documents.
/* 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>
usingnamespace 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
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.