Witam,
Dopiero zaczynam swoją przygodę z C++.
Dodam że program piszę w C++/CLI.
Chcę uruchomić przykład znaleziony w sieci.
#include "stdafx.h"
//using namespace std;
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;
void polacz(){
try {
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://192.168.8.2:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("insert into example values(4,'four'),(5, 'five')");
delete stmt;
pstmt = con->prepareStatement("select * from example");
res = pstmt->executeQuery();
while (res->next())
std::cout<<res->getInt("id")<<" "<<res->getString("data")<<std::endl;
delete res;
delete pstmt;
pstmt = con->prepareStatement("delete from example where id=?");
pstmt->setInt(1,4);
pstmt->executeUpdate();
pstmt->setInt(1,5);
pstmt->executeUpdate();
delete pstmt;
delete con;
}catch(sql::SQLException &e){
std::cout<<e.what();
std::cout << "# ERR: SQLException in " << __FILE__;
std::cout << "# ERR: " << e.what();
std::cout << " (MySQL error code: " << e.getErrorCode();
}
}
Zawartość pliku:
#pragma once
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
Bibliotekę MySQL linkowana jest dynamicznie, próbowałem statycznie ale nic to nie dało.
Ustawienia Visual Studio 2008 sp1 :
Project->Properties = Configuration Properties -> C/C++ -> General -> Additional Include Directory
C:\Program Files (x86)\MySQL\MySQL Connector C++ 1.0.5\include
Linker -> General -> Additional Library Directories
C:\Program Files (x86)\MySQL\MySQL Connector C++ 1.0.5\lib\opt
C:\MySQL\lib\opt
Linker -> Input -> Additional Dependencies
mysqlcppconn.lib
Błedy kompilacji:
1>------ Build started: Project: Studenci, Configuration: Debug Win32 ------
1>Linking...
1>Baza.obj : error LNK2031: unable to generate p/invoke for "extern "C" class sql::Driver * __clrcall get_driver_instance(void)" (?get_driver_instance@@$$J0YMPAVDriver@sql@@XZ); calling convention missing in metadata
1>stdafx.obj : error LNK2028: unresolved token (0A00000F) "public: virtual __clrcall sql::~SQLException(void)" (??1SQLException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::vector deleting destructor'(unsigned int)" (??_ESQLException@sql@@$$FUAMPAXI@Z) 1>stdafx.obj : error LNK2028: unresolved token (0A000011) "protected: virtual class sql::SQLException * __clrcall sql::SQLException::copy(void)" (?copy@SQLException@sql@@$$FMAMPAV12@XZ) referenced in function "void __clrcall
dynamic initializer for 'const sql::local vftable'''(void)" (???__E??_SSQLException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ) 1>stdafx.obj : error LNK2028: unresolved token (0A000013) "private: virtual class sql::SQLException * __clrcall sql::MethodNotImplementedException::copy(void)" (?copy@MethodNotImplementedException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall
dynamic initializer for 'const sql::local vftable'''(void)" (???__E??_SMethodNotImplementedException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ) 1>stdafx.obj : error LNK2028: unresolved token (0A000014) "public: virtual __clrcall sql::MethodNotImplementedException::~MethodNotImplementedException(void)" (??1MethodNotImplementedException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::MethodNotImplementedException::
vector deleting destructor'(unsigned int)" (??_EMethodNotImplementedException@sql@@$$FUAMPAXI@Z)
1>stdafx.obj : error LNK2028: unresolved token (0A000016) "private: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@InvalidArgumentException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::InvalidArgumentException::
local vftable'''(void)" (???__E??_SInvalidArgumentException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2028: unresolved token (0A000017) "public: virtual __clrcall sql::~InvalidArgumentException(void)" (??1InvalidArgumentException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::vector deleting destructor'(unsigned int)" (??_EInvalidArgumentException@sql@@$$FUAMPAXI@Z) 1>stdafx.obj : error LNK2028: unresolved token (0A000019) "private: virtual class sql::SQLException * __clrcall sql::InvalidInstanceException::copy(void)" (?copy@InvalidInstanceException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall
dynamic initializer for 'const sql::local vftable'''(void)" (???__E??_SInvalidInstanceException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ) 1>stdafx.obj : error LNK2028: unresolved token (0A00001A) "public: virtual __clrcall sql::InvalidInstanceException::~InvalidInstanceException(void)" (??1InvalidInstanceException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::InvalidInstanceException::
vector deleting destructor'(unsigned int)" (??_EInvalidInstanceException@sql@@$$FUAMPAXI@Z)
1>stdafx.obj : error LNK2028: unresolved token (0A00001C) "private: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@NonScrollableException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::NonScrollableException::
local vftable'''(void)" (???__E??_SNonScrollableException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2028: unresolved token (0A00001D) "public: virtual __clrcall sql::~NonScrollableException(void)" (??1NonScrollableException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::vector deleting destructor'(unsigned int)" (??_ENonScrollableException@sql@@$$FUAMPAXI@Z) 1>stdafx.obj : error LNK2028: unresolved token (0A000056) "public: virtual int __clrcall sql::mysql::MySQL_Savepoint::getSavepointId(void)" (?getSavepointId@MySQL_Savepoint@mysql@sql@@$$FUAMHXZ) referenced in function "void __clrcall
dynamic initializer for 'const sql::vftable'''(void)" (???__E??_7MySQL_Savepoint@mysql@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ) 1>stdafx.obj : error LNK2028: unresolved token (0A000057) "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __clrcall sql::mysql::MySQL_Savepoint::getSavepointName(void)" (?getSavepointName@MySQL_Savepoint@mysql@sql@@$$FUAM?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "void __clrcall
dynamic initializer for 'const sql::vftable'''(void)" (???__E??_7MySQL_Savepoint@mysql@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ) 1>Baza.obj : error LNK2028: unresolved token (0A000026) "public: int __clrcall sql::SQLException::getErrorCode(void)const " (?getErrorCode@SQLException@sql@@$$FQBMHXZ) referenced in function __catch$?polacz@@$$FYMXXZ$0 1>Baza.obj : error LNK2028: unresolved token (0A00004E) "extern "C" class sql::Driver * __clrcall get_driver_instance(void)" (?get_driver_instance@@$$J0YMPAVDriver@sql@@XZ) referenced in function "void __clrcall polacz(void)" (?polacz@@$$FYMXXZ) 1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual __clrcall sql::SQLException::~SQLException(void)" (??1SQLException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::SQLException::
vector deleting destructor'(unsigned int)" (??_ESQLException@sql@@$$FUAMPAXI@Z)
1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual __clrcall sql::~MethodNotImplementedException(void)" (??1MethodNotImplementedException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::vector deleting destructor'(unsigned int)" (??_EMethodNotImplementedException@sql@@$$FUAMPAXI@Z) 1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual __clrcall sql::InvalidArgumentException::~InvalidArgumentException(void)" (??1InvalidArgumentException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::InvalidArgumentException::
vector deleting destructor'(unsigned int)" (??_EInvalidArgumentException@sql@@$$FUAMPAXI@Z)
1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual __clrcall sql::~InvalidInstanceException(void)" (??1InvalidInstanceException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::vector deleting destructor'(unsigned int)" (??_EInvalidInstanceException@sql@@$$FUAMPAXI@Z) 1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual __clrcall sql::NonScrollableException::~NonScrollableException(void)" (??1NonScrollableException@sql@@$$FUAM@XZ) referenced in function "public: virtual void * __clrcall sql::NonScrollableException::
vector deleting destructor'(unsigned int)" (??_ENonScrollableException@sql@@$$FUAMPAXI@Z)
1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __clrcall sql::getSavepointName(void)" (?getSavepointName@MySQL_Savepoint@mysql@sql@@$$FUAM?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::mysql::MySQL_Savepoint::
vftable'''(void)" (???__E??_7MySQL_Savepoint@mysql@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "public: virtual int __clrcall sql::getSavepointId(void)" (?getSavepointId@MySQL_Savepoint@mysql@sql@@$$FUAMHXZ) referenced in function "void __clrcall dynamic initializer for 'const sql::mysql::MySQL_Savepoint::
vftable'''(void)" (???__E??_7MySQL_Savepoint@mysql@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "protected: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@SQLException@sql@@$$FMAMPAV12@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::SQLException::
local vftable'''(void)" (???__E??_SSQLException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "private: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@MethodNotImplementedException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::MethodNotImplementedException::
local vftable'''(void)" (???__E??_SMethodNotImplementedException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "private: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@InvalidArgumentException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::InvalidArgumentException::
local vftable'''(void)" (???__E??_SInvalidArgumentException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "private: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@InvalidInstanceException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::InvalidInstanceException::
local vftable'''(void)" (???__E??_SInvalidInstanceException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>stdafx.obj : error LNK2019: unresolved external symbol "private: virtual class sql::SQLException * __clrcall sql::copy(void)" (?copy@NonScrollableException@sql@@$$FEAMPAVSQLException@2@XZ) referenced in function "void __clrcall dynamic initializer for 'const sql::NonScrollableException::
local vftable'''(void)" (???__E??_SNonScrollableException@sql@@6B@@@YMXXZ@?A0xd1bbbeb3@@$$FYMXXZ)
1>Baza.obj : error LNK2019: unresolved external symbol "public: int __clrcall sql::getErrorCode(void)const " (?getErrorCode@SQLException@sql@@$$FQBMHXZ) referenced in function __catch$?polacz@@$$FYMXXZ$0
1>Baza.obj : error LNK2019: unresolved external symbol "extern "C" class sql::Driver * __clrcall get_driver_instance(void)" (?get_driver_instance@@$$J0YMPAVDriver@sql@@XZ) referenced in function "void __clrcall polacz(void)" (?polacz@@$$FYMXXZ)
1>C:\Users\Radekm\Documents\Visual Studio 2008\Projects\Studenci\Debug\Studenci.exe : fatal error LNK1120: 28 unresolved externals
1>Build log was saved at "file://c:\Users\Radekm\Documents\Visual Studio 2008\Projects\Studenci\Studenci\Debug\BuildLog.htm"
1>Studenci - 30 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
System jeśli to ma zanczenie Windows7 64bit
Pozdrawiam