参考这篇博客:http://www.tuicool.com/articles/mUZNne
配置:vs2010项目属性里面配置包含目录和库目录。
包含目录:C:\Program Files\MySQL\MySQL Server 5.6\include
库目录:C:\Program Files\MySQL\MySQL Server 5.6\lib
链接器-输入-附加依赖项 加入:libmysql.lib
把C:\Program Files\MySQL\MySQL Server 5.6\lib 下的 libmysql.lib 拷贝到 C:\Windows文件夹下
例子:连接test1数据库的emp表,输出表的所有内容
#include<iostream> #include "winsock.h" #include "mysql.h" using namespace std; int main() { MYSQL * mysql; mysql = mysql_init(NULL); char * sql = "select * from emp"; MYSQL_RES * result; MYSQL_ROW row; int r, num; if(mysql_real_connect(mysql, "localhost","root",NULL,"test1",3306,NULL,0)) { mysql_set_character_set(mysql,"gbk"); if(!mysql_query(mysql, sql)) { result = mysql_store_result(mysql); if(result == NULL) return 0; num = mysql_num_fields(result); unsigned long * lengths = mysql_fetch_lengths(result); while(row = mysql_fetch_row(result)) { for(r = 0; r < num; ++r) { cout<<row[r]<<‘ ‘; } cout<<endl; } mysql_free_result(result); } mysql_close(mysql); } return 0; }
函数在mysql中文参考手册的API部分有讲:http://www.cbi.pku.edu.cn/chinese/documents/csdoc/mysql/manual_Clients.html#C
时间: 2024-10-18 10:00:02