目录
如何连接到一个数据库
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
通过使用MySQL的客户端的mysql_options()函数来读取[客户端]和[你的程序名]在“my.cnf”文件中,用来确保你的程序是可用的,计时有人曾使用不是很标准的方式来设置过Mysql。
上面的程序片段中,mysql_real_connect()函数在5.0.3以后的版本中设置一个重连的标志位(MYSQL架构的一部分)为1.