我正在尝试将MySQL
数据库与Visual C++.
连接起来
我自己从MySQL的网站下载必要的文件。然后我按照网站提供的说明创建了我的项目。
现在当我点击Build然后它给了我错误。
Error 1 error C2059: syntax error : 'string' c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 2 error C2091: function returns function c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 3 error C2802: static member 'operator new' has no formal parameters c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 4 error C2333: 'sql::SQLException::operator new' : error in function declaration; skipping function body c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 5 error C2556: 'void *(__cdecl *sql::SQLException::operator new(void))(size_t,void *) throw()' : overloaded function differs only by return type from 'void *(__cdecl *sql::SQLException::operator new(void))(size_t) throw(std::bad_alloc)' c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 6 error C2556: 'void *(__cdecl *sql::SQLException::operator new(void))(size_t,const std::nothrow_t &) throw()' : overloaded function differs only by return type from 'void *(__cdecl *sql::SQLException::operator new(void))(size_t) throw(std::bad_alloc)' c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 7 error C2090: function returns array c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 8 error C2556: 'void *(__cdecl *sql::SQLException::operator new(void))(size_t,std::allocator<_Ty> &)' : overloaded function differs only by return type from 'void *(__cdecl *sql::SQLException::operator new(void))(size_t) throw(std::bad_alloc)' c:\program files\mysql\mysql connector c++ 1.1.7\include\cppconn\exception.h 98 1 MySQL_Connection
Error 9 error C1083: Cannot open include file: 'boost/scoped_ptr.hpp': No such file or directory C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\include\mysql_driver.h 30 1 MySQL_Connection
我检查了什么是错误,然后它显示错误在exception.h
。那条线是MEMORY_ALLOC_OPERATORS(SQLException)
。
此宏的代码如下。
#define MEMORY_ALLOC_OPERATORS(Class) \
void* operator new(size_t size) throw (std::bad_alloc) { return ::operator new(size); } \
void* operator new(size_t, void*) throw(); \
void* operator new(size_t, const std::nothrow_t&) throw(); \
void* operator new[](size_t) throw (std::bad_alloc); \
void* operator new[](size_t, void*) throw(); \
void* operator new[](size_t, const std::nothrow_t&) throw(); \
void* operator new(size_t N, std::allocator<Class>&);
可能违反了超载规则。
我该怎么做才能解决这个问题?我想只在c++
编写代码,而不是c#
。
整个文件:
// MySQL_Connection.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <stdio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define DB_HOST_NAME "localhost"
#define DB_USERNAME "root"
#define DB_PASSWORD "Passw0rd"
#define DB_SCHEMA_NAME "TESTDB"
//#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
// The one and only application object
using namespace std;
using namespace sql;
int main(int argc, char * argv[])
{
Driver *driver;
Connection *con;
ResultSet *rs;
Statement *stmt;
PreparedStatement *pre_stmt;
int id;
char* name;
char flag;
cout << "Connecting to MySQL database...";
/* Create a connection */
driver = get_driver_instance();
con = driver->connect(DB_HOST_NAME, DB_USERNAME, DB_PASSWORD);
try
{
if ( con->isValid() ) /* This method is used to check that mysql connection is alive or not */
{
/* Connect to mysql database */
con->setSchema(DB_SCHEMA_NAME);
/* Creating statement for inserting data into test table */
stmt = con->createStatement();
stmt->execute("USE " DB_SCHEMA_NAME );
stmt->execute("DROP TABLE IF EXISTS test");
stmt->execute("CREATE TABLE test ( id INT, name VARCHAR(30) )");
cout << "\nTable is created.";
cout << "\n\n Below Operations are done by \"Statement.\"";
stmt->execute("INSERT INTO test VALUES ( 1, 'Unnat' ) ");
stmt->execute("INSERT INTO test VALUES ( 2, 'Kunal' ) ");
stmt->execute("INSERT INTO test VALUES ( 3, 'Abhishek' ) ");
stmt->execute("INSERT INTO test VALUES ( 4, 'Mithun' ) ");
stmt->execute("INSERT INTO test VALUES ( 5, 'Kiran' ) ");
cout << "\nData Inserted.";
delete stmt;
/* Creating statement for fetching data from test table*/
stmt = con->createStatement();
rs = stmt->executeQuery("SELECT * FROM test ORDER BY id ASC");
cout << "ID\tNAME\n";
cout << "--\t----\n";
while ( rs->next() ) // next() will check weather next data is present or not
{
/*
* rs->getInt(x) : get the numbered column data, specified in parameter
[ NOTE ] : column number starts form 1, not 0.
* rs->getString ( "column-name" ) : fetch data of given column in parameter
*/
cout << rs->getInt(1) << rs->getString("name");
}
delete rs;
delete stmt;
/* Insert operation using PreparedStatement. */
cout << "\nInsert operation using PreparedStatement.\n\n";
do
{
pre_stmt = con->prepareStatement("INSERT INTO test VALUES ( ?, ? ) ");
cout << "ID : ";
cin >> id;
cout << "Name : ";
cin >> name;
pre_stmt->setInt ( 1, id );
pre_stmt->setString( 2, name );
cout << "Data Added.\n";
cout << "\nDo you want to continue? [Y/N] :";
cin >> flag;
} while (flag == 'Y' || flag == 'y');
delete pre_stmt;
}
}
catch (exception e)
{
cout << "Error :";
//cout << "Error on " << __LINE__ << " \n\t in ( " << __FUNCTION__ << " ) ";
e.what();
}
return 0;
}