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