It is impossible to make a request correctly, the function accepts a string ( "mysql("INSERT INTO history(path, date) VALUES ('" +ss+"','" + t + "')");" ) of type char , but in it cannot be summarized as in string . Tell me how you can fix the error

Expression must be integral or unscoped enum type.

 #define _LCC_ #define _CRT_SECURE_NO_WARNINGS #pragma comment(lib, "libmysql.lib") #include <iostream> #include <vector> #include <ctime> #include <stdio.h> #include <conio.h> #include <string> #include <clocale> //#include <sstream> #include <cstdlib> #include "MySQL Server 5.7\include\mysql.h" using namespace std; const int INF = INT_MAX / 2; const int n = 7; const int buf = 512; struct Edge { int start; int finish; int weight; }; vector<Edge> e; //Все ребра в графе vector<int> d(n, INF); //Расстояния от заданной вершины равны бесконечности vector<int> path[n]; //Все пути void mysql(const char query[]) { MYSQL mysql; // Дескриптор соединения MYSQL_ROW row; // Массив полей текущей строки MYSQL_RES* res; // Дескриптор результирующей таблицы MYSQL_FIELD* field; char host[] = "localhost"; // хост char user[] = "root"; // пользователь char passwd[] = "34784h0417"; // пароль char db[] = "test_db"; // название базы данных int port = 0; // порт. Если порт у сервера MySQL не по умолчанию (3306), то нужно указывать конкретный номер порта mysql_init(&mysql); // Инициализация mysql_real_connect(&mysql, host, user, passwd, db, port, NULL, 0); // соединение if (mysql_query(&mysql, query) > 0) { // Если была ошибка, ... printf("%s", mysql_error(&mysql)); // ... вывдем ее return; // и завершим работу } res = mysql_store_result(&mysql); // Берем результат, int num_fields = mysql_num_fields(res); // количество полей int num_rows = mysql_num_rows(res); // и количество строк. for (int i = 0; i < num_fields; i++) // Выводим названия полей { field = mysql_fetch_field_direct(res, i); // Получение названия текущего поля printf("| %s |", field->name); } printf("\n"); for (int i = 0; i < num_rows; i++) // Вывод таблицы { row = mysql_fetch_row(res); // получаем строку for (int l = 0; l < num_fields; l++) printf("| %s |", row[l]); // Выводим поля printf("\n"); } printf("Count records = %d", num_rows); // Вывод информации о количестве записей mysql_free_result(res); // Очищаем результаты mysql_close(&mysql); // Закрываем соединение } int main() { mysql("SELECT * FROM graph \n"); mysql("SELECT * FROM history \n"); int f, v; setlocale(0, ""); /*Ввод матрицы смежности*/ int G[n][n] = { { 0, 1, 2, 3, 4, 5, 6 } , { 1, 0, 2, 2, 5, 6, 7 } , { 2, 2, 0, 0, 4, 0, 0 } , { 3, 2, 0, 0, 2, 2, 0 } , { 4, 5, 4, 2, 0, 0, 2 } , { 5, 6, 0, 2, 0, 0, 8 } , { 6, 7, 0, 0, 2, 8, 0 } }; /*Заполнение матрицы*/ for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (G[i][j] != 0) { e.push_back({ i, j, G[i][j] }); } } } cout << "Введите начальную вершину: "; cin >> v; if (v != 1 && v != 2 && v != 3 && v != 4 && v != 5 && v != 6 && v != 7) { cout << "Неправильно введены данные \n"; } cout << "Введите конечную вершину: "; cin >> f; if (f != 1 && v != 2 && v != 3 && v != 4 && v != 5 && v != 6 && v != 7) { cout << "Неправильно введены данные \n"; } v--; d[v] = 0; float start_time = clock(); /*Алгоритм Форда-Беллмана*/ for (int i = 0; i < n; i++) path[i].push_back(v); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < e.size(); j++) { if (d[e[j].start] < INF) { if (d[e[j].finish] > d[e[j].start] + e[j].weight) { d[e[j].finish] = d[e[j].start] + e[j].weight; path[e[j].finish] = path[e[j].start]; path[e[j].finish].push_back(e[j].finish); } } } } float end_time = clock(); float search_time = end_time - start_time; cout << endl; /*Вывод*/ for (int i = 0; i < n; i++) { if (f == (i + 1)) { cout << "Наименьшее расстояние из вершины (" << v + 1 << ") в вершину (" << i + 1 << ") = " << d[i] << endl; cout << "Путь: " << path[i][0] + 1; for (int j = 1; j < path[i].size(); j++) { cout << "-" << path[i][j] + 1; } } cout << endl << endl; } cout << "Время работы алгоритма: " << search_time / CLOCKS_PER_SEC << "сек" << endl; auto a = time(NULL); auto t = asctime(localtime(&a)); char ss[128] = ""; cin >> ss; for (int i = 0; i < n; i++) { if (f == (i + 1)) { cout << path[i][0] + 1; for (int j = 1; j < path[i].size(); j++) { cout << "-" << path[i][j] + 1; } } cout << endl << endl; } mysql("INSERT INTO history(path, date) VALUES ('" + ss + "','" + t + "')"); system("pause"); return 0; } 
  • The error is given in the query string mysql, swears at ss - Vladislav

1 answer 1

Use the std::string class. For example,

 std::string values( "'" ); values += ss; values += "','"; values += t; values += "')"; mysql( ("INSERT INTO history(path, date) VALUES (" + values ).c_str()); 

Or you can select a closing bracket

 std::string values( "'" ); values += ss; values += "','"; values += t; values += "'"; ^^^ mysql( ("INSERT INTO history(path, date) VALUES (" + values + ")" ).c_str()); 
  • gives the same error - Vladislav
  • And everything figured out, thanks, works - Vladislav
  • @Vladislav Not at all. Ask more. :) - Vlad from Moscow