Connection error mysql_query

Asked

Viewed 54 times

0

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

MYSQL connection;
void data_insert();

int main(){

    mysql_init(&connection);

    if(mysql_real_connect(&connection,"localhost","root","irineuvocênãosabenemeu","teste",0,NULL,0)){
        std::cout << "Conexão com o banco de dados realizada com sucesso\n";
        data_insert();
    }else{
        std::cout << "ERRO: " << mysql_errno(&connection);
        std::cout << "\nNão foi possível realizar uma conexão com o servidor\n";
    exit(1);
    }

    return 0;
}

void data_insert(){

    std::string CPF;
    std::string nome;
    unsigned res;

    std::cout << "Insira seu CPF: ";
    getline(std::cin,CPF);
    std::cout << "Insira seu nome: ";
    getline(std::cin,nome);

    res=mysql_query(&connection,"INSERT INTO user(CPF, name) values('"+CPF+"','"+nome+"');"); //O erro está nesta linha

    if(res==0){
        std::cout << "Dados inseridos com sucesso\n";
        mysql_close(&connection);
    }else{
        system("clear");
        std::cout << "Ouve um erro ao inserir os dados, por favor tente novamente\n\n";
        main();
    }
}

How do I reverse the error in the above code?

error: cannot Convert ːStd::__cxx11::basic_string' to ?const char*' for argument ː2' to ːint mysql_query(MYSQL*, const char*)'|

1 answer

0

You reversed the order of the parameters.

res=mysql_query("INSERT INTO user(CPF, name) values('"+CPF+"','"+nome+"');", &connection);

According to the documentation, the Resource comes as second parameter.

  • 1

    The ending character is missing "

  • Agora deu esse error: cannot convert ‘std::__cxx11::basic_string<char>’ to ‘MYSQL* {aka st_mysql*}’ for argument ‘1’ to ‘int mysql_query(MYSQL*, const char*)’|

  • This question seems to be the same point you need: https://stackoverflow.com/questions/24438636/mysql-query-string-not-working-in-query-c

Browser other questions tagged

You are not signed in. Login or sign up in order to post.