-1
我正在用C++創建一個卡相關的程序,它使用了一個數據庫。在收到學生姓名和數字作爲輸入後,當我拒絕INSERT時,它停止。C++ MySQL插入錯誤?
▲DB建立明細(hakbun,名)
#include<iostream>
#include<string>
#include<stdio.h>
#include<mysql.h>
#include<conio.h>
#pragma comment(lib,"libmysql.lib")
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define DB_HOST "localhost"
#define DB_USER "root"
#define DB_PASS "1234"
#define DB_NAME "point_db"
void DBconnect() {
MYSQL *connection = NULL, conn;
MYSQL_RES *sql_result;
MYSQL_ROW sql_row;
mysql_init(&conn);
connection = mysql_real_connect(&conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 3306, (char *)NULL, 0);
if (connection == NULL){
printf("con error");
}
mysql_query(connection, "set session character_set_connection=euckr;");
mysql_query(connection, "set session character_set_results=euckr;");
mysql_query(connection, "set session character_set_client=euckr;");
}
int main()
{
MYSQL *connection = NULL, conn;
MYSQL_RES *sql_result;
MYSQL_ROW sql_row;
DBconnect();
int query_stat;
char query[1024];
char num;
while (1) {
cout << " +--------------------+" << endl;
cout << " | ① create card |" << endl;
cout << " +--------------------+" << endl;
cout << " input number > ";
cin >> num;
switch (num) {
case '1':
int i, hakbun, student;
char name;
cout << "student id : ";
cin >> hakbun;
cout << "mame : ";
cin >> name;
// insert
sprintf_s(query, "insert into point(hakbun, name) values('%d','%s')", hakbun, name);
query_stat = mysql_query(connection, query);
if (query_stat != 0){
printf("error : %s", mysql_error(&conn));
return 1;
}
default:
cout << "end program" << endl;
exit(0);
}
}
}
那麼,如何改變呢? 您必須輸入不同的字符,而不是單個字符。 –
@MinjuPark它應該是一個'char'數組或'std :: string'。 – Barmar
@MinjuPark我修改了答案 – AhmadWabbi