я не могу получить данные из базы данных mysql с помощью c ++ - PullRequest
1 голос
/ 06 мая 2020

эй, я новичок ie в c ++, я получил эту ошибку в моем коде, Ошибка: недопустимые операнды типов 'const char [35]' и 'const char [2]' для двоичного оператора + ' и я застрял в этих случаях, и это мой код

#include <iostream>
#include <bits/stdc++.h>
#include <windows.h>
#include <mysql.h> //header mysql
#include <sstream>  //merubah semua tipe data yang diinputkan user ke string
#include <algorithm> //untuk menggunakan fungsi begin, end
#include <regex>

using namespace std;

int main()
{
string hadis;
MYSQL* conn; //for connection handler
MYSQL_ROW row, row1; // return data as array of strings
MYSQL_RES* res; //hold the result set
int num_fields;
conn = mysql_init(0); //initialaize connection handler. dont change!
conn = mysql_real_connect(conn, "192.168.43.205", "ibrahim", "hadis", "hadis", 0, NULL, 0);
if(conn)
{
    int qstate = mysql_query(conn, "SELECT Isi_Indonesia FROM malik");
    if(!qstate)
    {
    res = mysql_store_result(conn); //
    num_fields = mysql_num_fields(res);
    while(row = mysql_fetch_row(res)) //mysqli fetch srray, Fetch all rows from the result
      {
      data = row[0];
      regex reg("[^\\w]+");
      data = regex_replace(data, reg, " ");
      //cout << data <<"\n\n";
      istringstream stm(data) ;
      while( stm >> hadis ) // read white-space delimited tokens one by one
       {
       for(int i = 0; i < num_fields; i++)
       {
       const char *hadiss = hadis.c_str();
       if(hadiss != NULL)
       {
       cout << hadiss << endl;
   /* 1st error */    mysql_query(conn,"SELECT kata FROM totalkata_malik2 WHERE kata='"+hadiss+"'"); //the 1st error
       res = mysql_store_result(conn);
       int num_fields = mysql_num_fields(res);
       while ((row = mysql_fetch_row(res)))
        {
 /* 2nd error */  string query2 = "INSERT INTO totalkata_malik2(kata,total) VALUES ('"+hadiss+"',1)"; // the code that got 2nd error
        mysql_query(conn, query2.c_str());
        }

         }

             else
             cout << "NULL" << endl;

               }
            }

        }
    }
}
return 0;
 }

Я попытался найти некоторые ссылки, но я не могу их понять, поэтому я не знаю, что мне делать, чтобы решить обе ошибки

Ответы [ 2 ]

0 голосов
/ 13 мая 2020

я нашел ответ с этим кодом

string query1 = "SELECT kata FROM totalkata_malik2 WHERE kata='"+hadis1+"'";
                        mysql_query(conn, query1.c_str());
0 голосов
/ 06 мая 2020

Вы не писали вторую ошибку, но, вероятно, хотите выполнить std :: string (hadiss), как в

string query2 = "INSERT INTO totalkata_malik2 (kata, total) VALUES ('" + std :: string (hadiss) + "', 1)";

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...