Połączenie z bazą danych mysql

0

Szukam jakiegoś tutoriala jak połączyć sią z bazą danych MySQL (znajduje się na innym hoscie) za pomocą c++ (visual studio), powinno to działać na windows i linux (kod) ewentualnie inne biblioteki. Zależy mi na tym aby nie instalować żadnego ustrojstwa dodatkowego do systemu typu serwer mysql itp. W Delphi jak i Lazurusie na Windows i Linux jest to bardzo łatwe do zrobienia są gotowe kontrolki + ściąga się jedną bibliotekę z mysql i chodzi. z c++ niestety jest problem, niby sporo sposobów ale pisanych dla ekspertów. Natomiast ja c/c++ znam trochę z mikrokontrolerów i chcę się go nauczyć, ponieważ delphi ma niestety pewne ograniczenia :/.

Nie wiem jaką bibliotekę siągnąć do c++ , jak ją podłączyć.

0

Do biblioteki MySQL dołączone są przykłady.

0

http://dev.mysql.com/downloads/connector/cpp/ pobierasz pod swój system kopiujesz dll do projektu a resztę linkujesz w VS.

0

Dzięki , natomiast w visual studio jestem laikiem i nie wiem jak poprawnie zaklinować ta "resztę"?

Jak dodam coś takiego :

 #include "lib/mysqlcppconn.dll"

to kompilator wywala mi sporo błędów:/

0

Może mi ktoś wyjaśnić gdzie robię błąd ?:

Visual Studio 2013 wersja 90 dniowa.

katalogi z plikami : include i lib z zipa:mysql-connector-c++-noinstall-1.1.5-win32
w ...\Visual Studio 2013\Projects\TestMySQL\TestMySQL\

W opcjach projektu -> Additional Include Directories dodałem ścieżkę C:\Users\Piotr\Documents\Visual Studio 2013\Projects\TestMySQL\TestMySQL\include;%(AdditionalIncludeDirectories)

kod source.cpp

#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
/* uncomment for applications that use vectors */
/*#include <vector>*/

#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>

#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "worlduser"
#define EXAMPLE_PASS "worldpass"
#define EXAMPLE_DB "world"

using namespace std;

int main(int argc, const char **argv)
{
	string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
	const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
	const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
	const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

	cout << "Connector/C++ tutorial framework..." << endl;
	cout << endl;

	try {

		/* INSERT TUTORIAL CODE HERE! */

	}
	catch (sql::SQLException &e) {
		/*
		MySQL Connector/C++ throws three different exceptions:

		- sql::MethodNotImplementedException (derived from sql::SQLException)
		- sql::InvalidArgumentException (derived from sql::SQLException)
		- sql::SQLException (derived from std::runtime_error)
		*/
		cout << "# ERR: SQLException in " << __FILE__;
		cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
		/* what() (derived from std::runtime_error) fetches error message */
		cout << "# ERR: " << e.what();
		cout << " (MySQL error code: " << e.getErrorCode();
		cout << ", SQLState: " << e.getSQLState() << " )" << endl;

		return EXIT_FAILURE;
	}

	cout << "Done." << endl;
	return EXIT_SUCCESS;
}

log z błędami kompilatora:

1>------ Build started: Project: TestMySQL, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\piotr\documents\visual studio 2013\projects\testmysql\testmysql\include\cppconn\sqlstring.h(39): warning C4251: 'sql::SQLString::realStr' : class 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' needs to have dll-interface to be used by clients of class 'sql::SQLString'
1>c:\users\piotr\documents\visual studio 2013\projects\testmysql\testmysql\include\cppconn\exception.h(61): warning C4251: 'sql::SQLException::sql_state' : class 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' needs to have dll-interface to be used by clients of class 'sql::SQLException'
1>c:\users\piotr\documents\visual studio 2013\projects\testmysql\testmysql\include\cppconn\exception.h(145): warning C4251: 'sql::SQLUnsupportedOptionException::option' : class 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' needs to have dll-interface to be used by clients of struct 'sql::SQLUnsupportedOptionException'
1>c:\users\piotr\documents\visual studio 2013\projects\testmysql\testmysql\include\mysql_connection.h(31): fatal error C1083: Cannot open include file: 'boost/shared_ptr.hpp': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
0

A którego słowa nie rozumiesz w zdaniu: - "Cannot open include file: 'boost/shared_ptr.hpp': No such file or directory"

0

Aktualnie mam taki błąd:

1>------ Build started: Project: TestMySQL, Configuration: Debug x64 ------
1>LINK : error LNK2001: unresolved external symbol Main
1>c:\users\piotr\documents\visual studio 2013\Projects\TestMySQL\x64\Debug\TestMySQL.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

z opcją debug, Z release jest więcej błędów

bibliotekę boost dodałem do katalogu :

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include

Natomiast bibliotekę mysql-connector-c++-noinstall-1.1.5-winx64 -> include oraz lib
przekopiowałem do katalogów:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib

kompilacja winx64

wydaje mi się, że brakuje jakiegoś pliku ?

0

Zainstalowałem wszystko od nowa na vm i teraz mam taki błąd:

------ Build started: Project: TestMySQL, Configuration: Debug Win32 ------
  Source.cpp
LINK : fatal error LNK1104: cannot open file 'mysqlcppconn-static.obj'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

tyle że nie ma nigdzie tych plików *.obj ?

0

Chodzi o mysqlcppconn-static.lib - sprawdź czy jest dodany do ścieżki bibliotek.

0

Ogólnie jest tak:

Linker > Input > Additional Dependencies jest tyko : %(AdditionalDependencies)

------ Build started: Project: TestMySQL, Configuration: Debug Win32 ------
Source.obj : error LNK2019: unresolved external symbol _get_driver_instance referenced in function _main
C:\Users\alien1983\Documents\Visual Studio 2013\Projects\TestMySQL_2\Debug\TestMySQL.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Natomiast jak jest tak: Linker > Input > Additional Dependencies jest tyko : mysqlcppconn-static;%(AdditionalDependencies)

 ------ Rebuild All started: Project: TestMySQL, Configuration: Debug Win32 ------
  Source.cpp
LINK : fatal error LNK1104: cannot open file 'mysqlcppconn-static.obj'
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Dodane linki do bibliotek:
Linker > Input > Additional Dependencies : mysqlcppconn-static;%(AdditionalDependencies)

Linker > General > Additional Library Directories : C:\Users\alien1983\Documents\Visual Studio 2013\Projects\TestMySQL_2\TestMySQL\lib;%(AdditionalLibraryDirectories)

c/c++ > General > Additional Include Directories : C:\Users\alien1983\Documents\Visual Studio 2013\Projects\TestMySQL_2\TestMySQL\include;%(AdditionalIncludeDirectories)

c/c++ > Preprocessor > Preprocessor Definitions : WIN32;_DEBUG;_CONSOLE;_LIB;CPPCONN_PUBLIC_FUNC=;%(PreprocessorDefinitions)

Paczka bibliotek pobrana z strony mysql to: mysql-connector-c++-noinstall-1.1.5-win32.zip

1 użytkowników online, w tym zalogowanych: 0, gości: 1