对于包含二进制数据的查询,你必须使用mysql_real_query()而不是mysql_query(),因为二进制代码数据可能包含“”字符,而且,mysql_real_query()比mysql_query()更快,因为它对查询字符串调用strlen() 。
2.3.3 返回值如果查询成功,零 。如果发生一个错误,非零 。
2.3.4 错误CR_COMMANDS_OUT_OF_SYNC
命令以一个不适当的次序被执行 。
CR_SERVER_GONE_ERROR
MySQL服务器关闭了 。
CR_SERVER_LOST
对服务器的连接在查询期间失去 。
CR_UNKNOWN_ERROR
发生一个未知的错误 。
2.4 mysql_store_result2.4.1 函数原型MYSQL_RES *mysql_store_result(MYSQL *mysql)
2.4.2 返回值A MYSQL_RES result structure with the results. NULL (0) if an error occurred.
2.5 mysql_fetch_row()DescriptionRetrieves the next row of a result set. When used after mysql_store_result(), mysql_fetch_row() returns NULLwhen there are no more rows to retrieve. When used after mysql_use_result(), mysql_fetch_row() returns NULLwhen there are no more rows to retrieve or if an error occurred.
The number of values in the row is given by mysql_num_fields(result). If row holds the return value from a call tomysql_fetch_row(), pointers to the values are accessed as row[0] to row[mysql_num_fields(result)-1]. NULL values in the row are indicated by NULL pointers.
The lengths of the field values in the row may be obtained by calling mysql_fetch_lengths(). Empty fields and fields containing NULL both have length 0; you can distinguish these by checking the pointer for the field value. If the pointer is NULL, the field is NULL; otherwise, the field is empty.
Return ValuesA MYSQL_ROW structure for the next row. NULL if there are no more rows to retrieve or if an error occurred.
ErrorsNote that error is not reset between calls to mysql_fetch_row()
- CR_SERVER_LOSTThe connection to the server was lost during the query.
- CR_UNKNOWN_ERRORAn unknown error occurred.
三、利用Mysql库提供的API编写连接Mysql和从Mysql中取出数据的代码#include <windows.h> 2: #include "stdio.h" 3: #include "winsock.h"4: #include "mysql.h"5:6:7: int main() 8: { 9:10: MYSQL * con; //= mysql_init((MYSQL*) 0);11: MYSQL_RES *res; 12: MYSQL_ROW row; 13:14:15: char tmp[400]; 16:17: //database configuartion 18: char dbuser[30]="root";19: char dbpasswd[30]="Apple"; 20: char dbip[30]="localhost"; 21: char dbname[50]="Excel"; 22: char tablename[50]="test"; 23: char *query=NULL; 24:25:26: int x; 27: int y; 28: int rt;//return value 29: unsigned int t; 30:31: int count = 0; 32:33:34: printf("input x,yn"); 35: scanf("%d,%d",&x,&y); 36: fflush(stdin); 37: printf("input overn"); 38: con = mysql_init((MYSQL*) 0);39:40:41: if ( con !=NULL && mysql_real_connect(con,dbip,dbuser,dbpasswd,dbname,3306/*TCP IP端口*/,NULL/*Unix Socket 连接类型*/,0/*运行成ODBC数据库标志*/) )42: {43: if (!mysql_select_db(con,dbname))44: {45: printf("Select successfully the database!n");46:47: con ->reconnect = 1;48:49: query = "set names 'GBK'"; 50: //mysql_query(con,"set names 'GBK'");51:52: rt=mysql_real_query(con,query,strlen(query)); 53: if (rt) 54: { 55: printf("Error making query: %s !!!n",mysql_error(con)); 56: } 57: else 58: { 59: printf("query %s succeed!n",query); 60: } 62: } 63: } 64:65: else 66: { 67: MessageBoxA(NULL,"Unable to connect the database,check your configuration!","",NULL); 68:69: } 70:71: //sprintf(tmp,"update %s set 商品='%s',卖出=%d,成交=%d,涨跌=%d,买进=%d,总量=%d,涨幅=%f,时间='%s' where %s",tablename,goods,sold,deal,fluctuate,buy,total,delta,time,UpdateCon); 72: sprintf(tmp,"insert into %s values(%s,%d,%d)",tablename,"null",x,y); //注意如何向具有自增字段的数据库中插入记录 73: //MessageBoxA(NULL,tmp,tmp,MB_OK); 74: //mysql_query(con,tmp); 75:76: rt=mysql_real_query(con,tmp,strlen(tmp)); 77: if (rt) 78: { 79: printf("Error making query: %s !!!n",mysql_error(con)); 80: } 81: else 82: { 83: printf("%s executed!!!n",tmp); 84: } 85:86: sprintf(tmp,"select * from %s",tablename); 87:88: rt=mysql_real_query(con,tmp,strlen(tmp)); 89: if (rt) 90: { 91: printf("Error making query: %s !!!n",mysql_error(con)); 92: }93: else 94: { 95: printf("%s executed!!!n",tmp); 96: } 97:98: res = mysql_store_result(con);//将结果保存在res结构体中 99:100: while(row = mysql_fetch_row(res))101: {102: /**103: * MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);104: * 检索行105: */106:107: for(t=0;t<mysql_num_fields(res);t++)108: {109: printf("%s ",row[t]);110: }111: printf(".............n");112: count ++; 113: }114: printf("number of rows %dn",count); 115: printf("mysql_free_result...n");116: mysql_free_result(res);117:118: mysql_close(con); 119: return 0; 120:121: }
推荐阅读
- CenterOS 7.5下Mysql Cluster 7.6.12高可用集群搭建
- c++多线程编程
- C++友元函数和友元类用法详解
- mysql 连接驱动问题
- pointer-like class C++|封装裸指针为像指针的类
- C++list的使用总结及常用list操作
- PHP如何像查询MySQL数据库一样查询数组内容
- MySQL查漏补缺
- MySQL 数据库铁律
- MySQL的5种时间类型的比较