由于某些原因我不知道,程序在尝试执行时会崩溃
mysql_query(mysql,"CREATE TABLE writers(name VARCHAR(25))");
虽然查询成功执行并且表格确实已经创建,但是窗口抱怨程序停止响应但是没有。
这是我的主要功能。
int main()
{
MYSQL* mysql;
mysql_init(mysql);
mysql_real_connect(mysql,"localhost","root","xxxx","test",0,NULL,0);
mysql_query(mysql,"CREATE TABLE writers(name VARCHAR(25))"); // 'Program stops responding' without actually crashing.
mysql_close(mysql);
getchar();
return 0;
}
答案 0 :(得分:7)
您对API的使用是错误的,您正在将悬空指针传递给mysql_init
而不会检查单个返回值。不要那样做。
您可以像这样转换代码:
MYSQL* mysql = mysql_init(NULL);
if (!mysql) {
// HANDLE THE ERROR
}
if (!mysql_real_connect(mysql,"localhost","root","xxxx","test",0,NULL,0)) {
// HANDLE THE ERROR
}
if (mysql_query(mysql,"CREATE TABLE writers(name VARCHAR(25))")) {
// HANDLE THE ERROR
}
mysql_close(mysql); // No error checking here, that's ok,
// it doesn't report errors
// But just for this call. All others => check.
getchar();
return 0;