Prepared Statement don't work and don't send error message in C++ with MySQL Connector 8 C

34 Views Asked by At

I'm trying to insert a record into a MySQL Database table, but when executing the piece of code below, after a long wait, the system finishes without showing any error and without printing Step 3.
Can someone help me?

              if (mysql_stmt_execute(stmt) != 0) {
                   std::cout << "Error excecute statement" << std::endl;
                   std::cout << mysql_error(connection) << std::endl;
                   mysql_close(connection);
                   exit(1);
               }
               std::cout << "Step 3" << std::endl;
               std::cout << "Inset Ok " << std::endl;

Below is the complete code:

#include <iostream>
#include <mysql.h>
#include <mysqld_error.h>

int main(){
    MYSQL *connection;
    char *sentence;
    const char *sql = "INSERT INTO doenca (cid, nome, descricao) VALUES (?, ?, ?)";
    MYSQL_STMT *stmt;
    const char *cid = "001";
    const char *nome = "flu";
    const char *descricao = "common respiratory inflammation";
    MYSQL_BIND bind[3];

    std::string sentenceInsert = "INSERT INTO doenca(cid, nome, descricao) VALUES (?, ?, ?)";
    if(!(connection = mysql_init(0))){
        std::cout << "Error connect MySQL" << std::endl;
    }
    else{
        if(!mysql_real_connect(connection, "localhost", "root", "admin", "pmudb",3306,  NULL, 0)){
            std::cout << "Error connect DB" << std::endl;
            std::cout << mysql_error(connection) << std::endl;
            mysql_close(connection);
            exit(1);
        }
        else{
            std::cout << "DB Connected." << std::endl;
            std::cout << sentenceInsert << std::endl;
            stmt = mysql_stmt_init(connection);
            if (!stmt) {
                std::cout << "Error create statement" << std::endl;
                std::cout << mysql_error(connection) << std::endl;
                mysql_close(connection);
                exit(1);
            }

            if (mysql_stmt_prepare(stmt, sql, strlen(sql)) != 0) {
                std::cout << "Error prepare statement" << std::endl;
                std::cout << mysql_error(connection) << std::endl;
                mysql_close(connection);
                exit(1);
            }
            std::cout << "Step 1" << std::endl;

            bind[0].buffer_type = MYSQL_TYPE_STRING; // Type 'cid'
            bind[0].buffer = (void *)cid; // Pointer para 'cid'

            bind[1].buffer_type = MYSQL_TYPE_STRING; // Type 'nome'
            bind[1].buffer = (void *)nome; // Pointer 'nome'

            bind[2].buffer_type = MYSQL_TYPE_STRING; // Type 'descricao'
            bind[2].buffer = (void *)descricao; // Pointer para 'descricao'

            if (mysql_stmt_bind_param(stmt, bind) != 0) {
                std::cout << "Error binding parameters" << std::endl;
                std::cout << mysql_error(connection) << std::endl;
                exit(1);
            }

            std::cout << "Step 2" << std::endl;
            std::cout << "Param cid: " << (const char *)bind[0].buffer << std::endl;
            std::cout << "Param nome: " << (const char *)bind[1].buffer << std::endl;
            std::cout << "Param descricao: " << (const char *)bind[2].buffer << std::endl;

            if (mysql_stmt_execute(stmt) != 0) {
                std::cout << "Error excecute statement" << std::endl;
                std::cout << mysql_error(connection) << std::endl;
                mysql_close(connection);
                exit(1);
            }
            std::cout << "Step 3" << std::endl;
            std::cout << "Inset Ok " << std::endl;
        }
    }
    mysql_stmt_close(stmt);
    mysql_close(connection);
    return 0;
}

It compile right and run with no error, but it doesn't execute all sentences.

It prints until the Step 2 and don't make anything at Step3 .

0

There are 0 best solutions below