How to save records from a mysql query in C ++

0

I'm using a C ++ library that does the query in the database. My goal is to do a query, and save the data in a txt file, maybe even manipulating it to get json format. The problem is that I can not retrieve the value of the records when saving the file. When I add the row [i] value, it saves a binary value, rather than ASCII characters. Apparently, even if it was in ASCII, I doubt it would return the contents of the field, since the mysql_fetch_row function probably does not do this. I could not find any fetchArray function if it exists. Follow the code

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

using namespace std;

#define HOST "localhost"
#define USER "root"
#define PASS ""
#define DB "estoque"


MYSQL *conn;


int main()
{

    conn = mysql_init(NULL);



    if(mysql_real_connect(conn, HOST, USER, PASS, DB, 0, NULL, 0))
    {
        cout << "conectado \n";

    }
    //Select all table
    mysql_query(conn,"SELECT * FROM estoque"); 

    snprintf(buf, sizeof buf, "SELECT %s from %s", value, table);

    mysql_real_query(conn, buf, strlen(buf)); 

    // pointer of result
    MYSQL_RES *result; 

    MYSQL_ROW row; 
    unsigned int i;


    result = mysql_store_result(conn); 
    row = mysql_fetch_row(result);


    while ((row = mysql_fetch_row(result)) != NULL) 
    {   
    for (i=0; i<mysql_num_fields(result); i++) 

        cout << row[i] << endl;

    //  ############### ESCREVER EM ARQUIVO DE TEXTO O RESULTADO ############################

        std::ofstream Hypnos_FILE;
std::string TEXTO = row[i];
Hypnos_FILE.open("resultado.txt", std::ios::app);
if (Hypnos_FILE.is_open())
{
   std::cout << "Arquivo de texto aberto com sucesso!\n";

   Hypnos_FILE << TEXTO;

}
else
   std::cout << "Erro ao abrir arquivo de texto.";

Hypnos_FILE.close();                

    //  ############### ESCREVER EM ARQUIVO DE TEXTO O RESULTADO ############################

    } 
    // mysql_query(conn,"SELECT varchar1, varchar2 FROM estoque;");
    return 0;
}
    
asked by anonymous 04.08.2017 / 00:02

1 answer

1

First you need to check return values properly, starting with mysql_init() and mysql_real_connect() , and abort the program if necessary. If this does not happen, the result may be unexpected and may result in invalid data being written to your file just as it is already.

Second you are discarding the first row of the table, as there is a call to the mysql_fetch_row() function in the window.

Finally, and maybe the real reason for your problem, you're using the index (variable i ) with an invalid value, causing a buffer overflow . Just put the file's writing within the scope of your for loop to solve this problem.

Here's a simple code that works the way you probably want:

#include <iostream>
#include <fstream>

#include <string.h>

#include <mysql/mysql.h>

using namespace std;

#define HOST "localhost"
#define USER "root"
#define PASS ""
#define DB "estoque"

MYSQL *conn;

int main()
{
    MYSQL *conn = mysql_init(NULL);
    if (!conn) {
        cout << "falha no mysql_init()" << endl;
        return 1;
    }

    conn = mysql_real_connect(conn, HOST, USER, PASS, DB, 0, NULL, 0);
    if (!conn) {
        cout << "falha no mysql_real_connect()" << endl;
        return 1;
    }

    cout << "conectado" << endl;

    const char *value = "*";
    const char *table = "banana";
    char buf[128];
    snprintf(buf, sizeof buf, "SELECT %s from %s", value, table);
    mysql_real_query(conn, buf, strlen(buf));

    MYSQL_ROW row;
    MYSQL_RES *result = mysql_store_result(conn);

    std::ofstream Hypnos_FILE;
    Hypnos_FILE.open("resultado.txt", std::ios::app);
    if (!Hypnos_FILE.is_open()) {
        std::cout << "Erro ao abrir arquivo de texto.";
        return 1;
    }
    std::cout << "Arquivo de texto aberto com sucesso!\n";

    unsigned int j = 0;
    while (((row = mysql_fetch_row(result)) !=NULL)) {
        cout << j << ":";
        Hypnos_FILE << j << ":";;
        j++;
        for (unsigned int i = 0; i < mysql_num_fields(result); i++) {
            cout << " " << row[i];
            Hypnos_FILE << " " << row[i];
        }
        cout << endl;
        Hypnos_FILE << endl;
    }

    Hypnos_FILE.close();

    mysql_close(conn);

    return 0;
}
    
04.08.2017 / 00:55