mysql学习
#include "include/mysql.h" #include <iostream> #include <assert.h> int main() { MYSQL mysql; //初始化 mysql_init(&mysql); //连接 assert(0 != mysql_real_connect(&mysql, "localhost", "root", "zzcan728", "whdb", 3306, NULL, 0)); //查询 char sql[1024] = {0}; sprintf_s(sql, 1024, "SELECT `user_id`, `name`, `pic`, `vip`" "FROM `user`" "WHERE `user_id` = '1'"); int code = mysql_real_query(&mysql, sql, strlen(sql)); if (0 != code) //非0查询失败 { printf("query failed! [%s] [%d] [%s] \n", sql, mysql_errno(&mysql), mysql_error(&mysql)); return false; } //保存查询结果 MYSQL_RES* res = mysql_store_result(&mysql); MYSQL_ROW row; if (row = mysql_fetch_row(res))//检索结果集的下一行,如果没有的话返回null { unsigned long* lengths = mysql_fetch_lengths(res);//取得每一行中每个字段的内容长度 for (int i = 0; i < mysql_num_fields(res); ++i)//获取行中字段数量 { std::cout << row[i] << lengths[i] << std::endl; } } mysql_free_result(res); mysql_close(&mysql); getchar(); return 0; }