当前位置:Gxlcms >
数据库问题 >
MYSQL C API : mysql_real_connect()
MYSQL C API : mysql_real_connect()
时间:2021-07-01 10:21:17
帮助过:2人阅读
#include <iostream>
2 #include <mysql.h>
3 #include <
string>
4
5 #include <assert.h>
6
7 int main()
8 {
9 MYSQL *ms_conn =
mysql_init(NULL);
10 if (ms_conn ==
NULL)
11 {
12 std::cout <<
"Error: mysql_init failed." <<
std::endl;
13 return 0;
14 }
15 std::cout <<
"Info: mysql_init successful." <<
std::endl;
16
17 MYSQL *ms_res =
NULL;
18 ms_res = mysql_real_connect(ms_conn,
"localhost",
"root",
"123456",
19 "db_name",
0, NULL,
0);
20 if (ms_res ==
NULL)
21 {
22 std::cout <<
"Error: connect mysql failed: " << mysql_error(ms_conn) <<
std::endl;
23 mysql_close(ms_conn), ms_conn =
NULL;
24 return 0;
25 }
26 std::cout <<
"Info: mysql connect successful." <<
std::endl;
27
28 // ... // 其他操作
29
30 // 使用完释放系统资源
31 mysql_close(ms_conn), ms_conn =
NULL;
32 }
MYSQL C API : mysql_real_connect()
标签: