小白一个,今天写代码MySQL连接不上,我也不知道哪里出现了问题,数据库test已建立
#include<iostream>
#include<WinSock.h>
#include<cstdio>
#include<string>
#include<conio.h>
#include<mysql.h>
#pragma comment (lib,"Wsock32.lib")
#pragma comment (lib,"libmysql.lib")
using namespace std;
char szSqlText[1000];
int main(int argc, char* argv)
{
char host[] = "localhost";
char szTargetDSN[] = "text";
const char username[] = "root";
const char password[] = "****";
unsigned int port = 3306;
MYSQL* conn;
MYSQL_RES* res;
MYSQL_ROW row;
if (
(conn = mysql_init((MYSQL*)0))
&&
(mysql_options(conn, MYSQL_SET_CHARSET_NAME, "gbk") == 0)
&&
mysql_real_connect(conn, host, username, password, szTargetDSN, port, NULL, 0)
)
{
mysql_query(conn, "updata tbl_count set num=num+1");
strcpy_s(szSqlText, "select num from tbl_count limit 1");
if (mysql_query(conn, szSqlText))
{
mysql_close(conn);
printf("读取失败\n");
return false;
}
else
{
res = mysql_store_result(conn);
row = mysql_fetch_row(res);
if (row != NULL)
printf("本程序是第%d次运行\n", row[0]);
else
printf("未获取行\n");
}
mysql_free_result(res);
printf("\n按任意键退出\n");
_getch();
}
else {
printf("\n连接数据库失败\n");
mysql_close(conn);
printf("\n按任意键退出\n");
_getch();
return false;
}
mysql_close(conn);
return true;
}
![这是运行的结果,显示数据库连接失败](https://img-blog.csdnimg.cn/20200422221445816.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpcV83,size_16,color_FFFFFF,t_70)