2016-06-07 18 views
2

Ich möchte meinen Raspberry Pi 2 mit Hilfe von C++ mit einer externen MySQL-Datenbank auf 000webhost.com verbinden.Verbinden des Raspberry Pi 2 mit einer externen Remote-Datenbank

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

using namespace std; 

int main(void) 
{  
try { 
    sql::Driver *driver; 
    sql::Connection *con; 
    sql::Statement *stmt; 
    sql::ResultSet *res; 

    /* Create a connection */ 
    driver = get_driver_instance(); 
    con = driver->connect("<000webhost mysql address>", "<username>", "<password>"); 
    /* Connect to the MySQL test database */ 
    con->setSchema("<database>"); 

    stmt = con->createStatement(); 
    res = stmt->executeQuery("<sql statement>"); // replace with your statement 
    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; 
} 

Ich habe eine Fehlermeldung, dass die mysql_connection.h Datei fehlt. Ich weiß nicht, was ich falsch gemacht habe oder ob es einen leichteren oder einfacheren Weg gibt. Bitte helfen Sie mir bei dieser Angelegenheit. Vielen Dank.

+0

Haben Sie die [Quelle installiert] (https: //dev.mysql.com/doc/connector-cpp/en/connector-cpp-installation-source.html), damit mysql_connection.h auf Ihrem Raspberry Pi funktioniert? – NonCreature0714

+0

Ich habe mysql nicht auf dem Raspberry Pi installiert, seit ich versucht habe, auf eine externe Datenbank zuzugreifen. Ich bin neu in der Raspberry Pi-Umgebung. –

+0

Beachten Sie, dass ich nicht gefragt habe, ob MySQL installiert ist, sondern die Quelle für die MySQL-Connector-C++ - Bibliothek, die nicht MySQL ist. Sie benötigen die Bibliothek, die dasselbe ist wie ein Paket in Python, aber für C++, damit mysql_connection.h funktioniert. – NonCreature0714

Antwort

0

Ich habe eine Fehlermeldung erhalten, dass die Datei mysql_connection.h fehlte.

durch die Fehlermeldung Gehen Sie beschreiben, und die Antwort auf meine Frage? „Haben Sie die Quelle für mysql_connection.h installiert“, das war:

ich nicht mysql installiert haben auf die Raspberry Pi seit ich versucht habe auf eine externe Datenbank zuzugreifen.

(Was nicht ist, was, fragte ich.) es erscheint der Grund #include "mysql_connection.h ist nicht funktioniert, weil die Bibliothek nicht vorhanden ist.

Die Connector/C Bibliothek nicht erfordern die Installation von MySQL, aber die Connector/C Quelle muss installiert werden Header aus der Bibliothek enthalten, according to the documentation.

MySQL Connector/C Bibliothek erfordertboost; Allerdings hat Raspian bereits Boost installiert, obwohl das mit seinem eigenen Problemsatz kommen kann, die besprochen und gelöst werden here, wenn Sie Probleme mit Boost haben. (Wenn Ihr Raspberry Pi NOOBS hat, empfehle ich, zu Raspian zu wechseln.)

Obwohl der Link in meinen Kommentaren ist, sind Anweisungen zum Installieren der Connector/C-Bibliothek von MySQL's documentation verfügbar.

Ich habe einige Ressourcen und Links gesammelt, die einige zusätzliche Hilfe bieten sollte:

  1. How to Install Third Party Libraries
  2. How to #include third party libraries
  3. Installing Connector/C++ from Source on Unix and Unix-Like Systems