Eclipse+CDT环境下使用C语言连接MySQL详细讲解

2014-11-24 17:39:08 · 作者: · 浏览: 0

#ifndef MYSQLCONN_H_
#define MYSQLCONN_H_


int TestMySQLConn();



#endif /* MYSQLCONN_H_ */


创建一个程序文件MySQLConn.c,代码如下:
/*
* MySQLConn.c
*
* Created on: Jun 11, 2009
* Author: zhanglei
*/
#include
#include
#include
#include


#include "MySQLConn.h"


int main(int agrc, char* argv[])
{
printf("MySQL Connection Test...");


return TestMySQLConn();
}


int TestMySQLConn()
{
MYSQL *pConn;
//MYSQL_RES *pRes;
//MYSQL_ROW my_conn;
pConn = mysql_init(NULL);
if(NULL == pConn)
{
printf("mysql_init failed!");
return EXIT_FAILURE;
}


pConn = mysql_real_connect(pConn, "127.0.0.1", "root", "123", "test", 3306, NULL, 0);


if(NULL == pConn)
{
printf("Connection failed!");
}
else
{
printf("Connection succeed!");
}


mysql_close(pConn);


return EXIT_SUCCESS;
}