Mysql安装包中提供了自己的API,使用前需要添加几个东西,右键新建的项目,选属性,做如下3点设置。
我的MySQL安装目录是E:Program FilesMySQLMySQL Server 6.0.....,大家根据自己安装目录改一下就成了。
1. 设置“附加包含目录”
2、设置附加库目录
3、填好附加依赖项
下面就直接上代码吧~~:
注:程序里面用到的函数官网都有详细的说明和使用文档,
详情戳这里:http://dev.mysql.com/doc/refman/5.1/zh/apis.html#c-api-functions
#include <windows.h> #include <winsock.h> #include <mysql.h> #include <iostream> #include <string> using namespace std; #define DEBUG int main(int argv, char* arvs[]) { MYSQL mydata; if(mysql_library_init(0, NULL, NULL) == 0) { cout << "mysql_library_init() succeed" << endl; } else { cout << "mysql_library_init() failed" << endl; return -1; } #ifdef DEBUG system("pause"); #endif if(mysql_init(&mydata) != nullptr) { cout << "mysql_init() succeed" << endl; } else { cout << "mysql_init() failed" << endl; return -1; } #ifdef DEBUG system("pause"); #endif if(mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk") == 0) { cout << "mysql_options() succeed" << endl; } else { cout << "mysql_options() failed" << endl; return -1; } #ifdef DEBUG system("pause"); #endif if(mysql_real_connect(&mydata, "localhost", "root", "momo", "adserving", 3306, NULL, 0) != nullptr) { cout << "mysql_real_connect() succeed" << endl; } else { cout << "mysql_real_connect() failed" << endl; return -1; } #ifdef DEBUG system("pause"); #endif string sqlstr; sqlstr = "create table if not exists user_info ( user_id int unsigned not null auto_increment primary key comment 'Unique User ID', user_name varchar(100) character set gb2312 collate gb2312_chinese_ci NULL comment 'Name of User', user_second_sum int unsigned not null default 0 comment 'The Sumation of Using Time' )"; if(mysql_query(&mydata, sqlstr.c_str()) == 0) { cout << "mysql_query() create table succed" << endl; } else { cout << "mysql_query() create table failed" << endl; mysql_close(&mydata); return -1; } #ifdef DEBUG system("pause"); #endif sqlstr = "insert into user_info values (1, '中文', 11)"; if(mysql_query(&mydata, sqlstr.c_str()) == 0) { cout << "mysql_query() insert data succeed" << endl; } else { cout << "mysql_query() insert data failed" << endl; mysql_close(&mydata); return -1; } #ifdef DEBUG system("pause"); #endif sqlstr = "select user_id, user_name, user_second_sum from user_info"; MYSQL_RES *result = nullptr; if(mysql_query(&mydata, sqlstr.c_str()) == 0) { cout << "mysql_query() select data succeed" << endl; result = mysql_store_result(&mydata); int rowcount = mysql_num_rows(result); cout << "row count: " << rowcount << endl; size_t fieldcount = mysql_num_fields(result); MYSQL_FIELD *field = NULL; for(size_t i = 0; i < fieldcount; i++) { field = mysql_fetch_field_direct(result, i); cout << field->name << " "; } cout << endl; MYSQL_ROW row = nullptr; row = mysql_fetch_row(result); while(row != nullptr) { for(int i = 0; i < fieldcount; i++){ cout << row[i] << " "; } cout << endl; row = mysql_fetch_row(result); } } else { cout << "mysql_query() select data failed" << endl; mysql_close(&mydata); return -1; } #ifdef DEBUG system("pause"); #endif sqlstr = "drop table user_info"; if(mysql_query(&mydata, sqlstr.c_str()) == 0) { cout << "mysql_query() drop table succed" << endl; } else { cout << "mysql_query() drop table failed" << endl; mysql_close(&mydata); return -1; } mysql_free_result(result); mysql_close(&mydata); mysql_server_end(); system("pause"); return 0; }
运行结果如下图: