MYSQL * mysql_real_connect(
MYSQL *mysql,
const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long clientflag);
// 连接到MYSQL 数据库服务器 在头文件mysql.h 中声明
// 参数的说明请参考百度百科
代码范例:
#include <iostream>
#include <mysql.h>
#include <string> #include <assert.h> int main()
{
MYSQL *ms_conn = mysql_init(NULL);
if (ms_conn == NULL)
{
std::cout << "Error: mysql_init failed." << std::endl;
return ;
}
std::cout << "Info: mysql_init successful." << std::endl; MYSQL *ms_res = NULL;
ms_res = mysql_real_connect(ms_conn, "localhost", "root", "",
"db_name", , NULL, );
if (ms_res == NULL)
{
std::cout << "Error: connect mysql failed: " << mysql_error(ms_conn) << std::endl;
mysql_close(ms_conn), ms_conn = NULL;
return ;
}
std::cout << "Info: mysql connect successful." << std::endl; // ... // 其他操作 // 使用完释放系统资源
mysql_close(ms_conn), ms_conn = NULL;
}