alterneuersack
Goto Top

Mysql Connector C++ Verbindungsproblem

Hallo,
ich arbeite mit Visual Studio 2022 und möchte auf meine Mysql Datenbank zugreifen.
Ich habe alles von dem Connector 8.0.33 includiert.
Datenbank und Tabelle ist vorhanden.

Trotzdem sagt er mir er kann die Tabelle test1 nicht finden.
Woran kann das liegen?

Release

Projekt/Eigenschaften/c/c++/Allgemein/Zusätzliche Includeverzeichnisse: mysql8.0.33-64/include
Projekt/Eigenschaften/c/c++/Präprozessor/Präprozessordefenitionen: STATIC_CONCPP;

Projekt/Eigenschaften/Linker/Eingabe/Zusätzliche Abhängigkeiten: mysqlcppconn-static.lib
Projekt/Eigenschaften/Linker/Allgemein/Zusätzliche Bibliotheksverzeichnisse: mysql8.0.33-64/lib64/vs14

Fehler Meldung:

Connector/C++ tutorial framework...

  1. ERR: SQLException in C:\Users\deddy\source\repos\MysqlProject1\main.cpp(main) on line 58
  2. ERR: PROCEDURE test.add_test1 does not exist (MySQL error code: 1305, SQLState: 42000 )

C:\Users\deddy\source\repos\MysqlProject1\x64\Release\MysqlProject1.exe (Prozess "20628") wurde mit Code "1" beendet.
Drücken Sie eine beliebige Taste, um dieses Fenster zu schließen.


Line 58: cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;


C++ Code:

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

#include <jdbc/mysql_driver.h>
#include <jdbc/mysql_connection.h>

#include <jdbc/cppconn/driver.h>
#include <jdbc/cppconn/exception.h>
#include <jdbc/cppconn/resultset.h>
#include <jdbc/cppconn/statement.h>
#include <jdbc/cppconn/prepared_statement.h>

#define EXAMPLE_HOST "localhost" 
#define EXAMPLE_USER "root" 
#define EXAMPLE_PASS "" 
#define EXAMPLE_DB "test" 

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! */


        sql::Driver* driver = get_driver_instance();
        std::auto_ptr<sql::Connection> con(driver->connect(url, user, pass));
        con->setSchema(database);
        std::auto_ptr<sql::Statement> stmt(con->createStatement());

        // We need not check the return value explicitly. If it indicates
        // an error, Connector/C++ generates an exception.
        stmt->execute("CALL add_test1('A', 'B', 'C')");  

    }
    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;
}

Schon mal Danke für Hinweise oder Lösungsmöglichkeiten.

Content-Key: 7339294173

Url: https://administrator.de/contentid/7339294173

Printed on: April 27, 2024 at 22:04 o'clock

Member: HansFenner
HansFenner May 28, 2023 updated at 17:23:53 (UTC)
Goto Top
Hallo,

Das ist eine Fehlermeldung von MySQL und die besagt nicht, dass die Tabelle test1 nicht gefunden werden kann, sondern, dass die Prozedure test.add_test1 nicht gefunden werden konnte.

Könnte auch daran liegen, dass du nicht die nötigen Rechte hast. Einfach mal nach "MySQL error code: 1305" suchen.
Member: Dawnbreaker
Dawnbreaker May 28, 2023 at 18:11:25 (UTC)
Goto Top
Hallo,

In Line 46 rufst du folgenden Code auf:
 stmt->execute("CALL add_test1('A', 'B', 'C')");  

Dass bedeutet, dass du die Stored Procedure add_test1 aufrufst. Wenn diese aber nicht in der DB existiert, bekommst du ebendiese Fehlermeldung zurück.

Kurzum: Deine Verbindung zur Datenbank funktioniert, dein Code aber nicht.

LG
Member: AlterNeuerSack
AlterNeuerSack May 29, 2023 at 09:30:28 (UTC)
Goto Top
Danke, dann werde ich daran weiter lernen, denn es fehlt mir immer noch das Verständnis. Wenn ich eine Lösung gefunden habe Poste ich sie.
Member: AlterNeuerSack
Solution AlterNeuerSack May 29, 2023 at 14:04:54 (UTC)
Goto Top
Ich habe es im try Block so gelöst, viel einfacher. Und funktioniert!

try {

        /* INSERT TUTORIAL CODE HERE! */

        sql::mysql::MySQL_Driver* driver;
        sql::Connection* con;
        sql::Statement* stmt;

        driver = sql::mysql::get_mysql_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "root", "");  

        stmt = con->createStatement();
        stmt->execute("USE " EXAMPLE_DB);  
        stmt->execute("DROP TABLE IF EXISTS test");  
        stmt->execute("CREATE TABLE test(id INT, label CHAR(1))");  
        stmt->execute("INSERT INTO test(id, label) VALUES (1, 'a')");  

        delete stmt;
        delete con;

    }