min_jeong min_jeong - 29 days ago 5
C++ Question

DB (MYSQL) command to query in c ++


sprintf_s(query, "update point set p%d=%d where hakbun='%d';", no, point, hakbun);



The data in the database is not output for the reason of the sentence.

What is the problem?

void updatePoint(MYSQL *con) {

MYSQL * connection = NULL, conn;
MYSQL_RES * sql_result;
MYSQL_ROW sql_row;

int hakbun, field, j, query_stat, no, point;
char query[1024];

cout << "student id > ";
cin >> hakbun;

cout << " ┌────────────────────────────┐" << endl;
cout << " └────────────────────────────┘" << endl;
cout << " num > ";
cin >> no;
cout << " point > ";
cin >> point;

sprintf_s(query, "update point set p%d='%d' where hakbun='%d';", no, point, hakbun);
sprintf_s(query, "select * from point where hakbun = '%d';", hakbun);
cout << sizeof(query) << endl;
query_stat = mysql_query(connection, query);
sql_result = mysql_store_result(connection);
while ((sql_row = mysql_fetch_row(sql_result)) != NULL) {
void table();
printf(" %s\t %s  %s   %s  %s  %s\t%s\t%s\t %s\t%s\t%s\n", sql_row[0], sql_row[1], sql_row[2], sql_row[3], sql_row[4], sql_row[5], sql_row[6], sql_row[7], sql_row[8], sql_row[9], sql_row[10]);
}
mysql_free_result(sql_result);
}

Answer

You need to execute the update query before you reassign query with the select query.

You're also missing the second argument to sprintf_s(), which should contain the size of the query buffer. And when you call table(), you shouldn't put void before that -- that makes it a function declaration, not a function call.

You're using the variable connection in your mysql_query() calls, but you never assigned that to a MySQL connection. Your function takes a connection as a parameter in con, you should use that instead.

sprintf_s(query, sizeof(query), "update point set p%d='%d' where hakbun='%d';", no, point, hakbun);
mysql_query(con, query);
sprintf_s(query, sizeof(query), "select * from point where hakbun = '%d';", hakbun);
mysql_query(con, query);
sql_result = mysql_store_result(con);
while ((sql_row = mysql_fetch_row(sql_result)) != NULL) {
    table();
    printf("  %s\t %s  %s   %s  %s  %s\t%s\t%s\t %s\t%s\t%s\n", sql_row[0], sql_row[1], sql_row[2], sql_row[3], sql_row[4], sql_row[5], sql_row[6], sql_row[7], sql_row[8], sql_row[9], sql_row[10]);
}
mysql_free_result(sql_result);
Comments