Home > Database > Mysql Tutorial > MySQL学习笔记_13_Linux下C++/C连接MySQL数据库(三)--处理返回数_MySQL

MySQL学习笔记_13_Linux下C++/C连接MySQL数据库(三)--处理返回数_MySQL

WBOY
Release: 2016-06-01 13:18:01
Original
1023 people have browsed it

Linux学习笔记

bitsCN.com

Linux下C++/C连接MySQL数据库(三)

--处理返回数据

一、通过返回结果集中的字段数

unsigned int mysql_field_count(MYSQL * connection);//将MYSQL_ROW的值作为一个存储了一行数据的数组...
Copy after login

示例:

//一次取一个值的情况,另一种情况与其类似,修改处会标出#include <iostream>#include <fstream>#include <cstdlib>#include <mysql/mysql.h>using namespace std;void mysql_err_function(MYSQL * connection);void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow);int main(){    MYSQL * connection;    connection = mysql_init(NULL);    if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))    {        cout << "Connection to MySQL Server is Succeed..." << endl;        string query = "select * from tmp15";        //getline(cin,query);        int res = mysql_query(connection,query.c_str());        if (res)        {            mysql_err_function(connection);        }        else        {            MYSQL_RES * my_res = mysql_use_result(connection);	//将mysql_use_result改为mysql_store_result即可得到另一种情况的结果(其实是相同的...)            if (my_res)            {                MYSQL_ROW sqlrow;                while ((sqlrow = mysql_fetch_row(my_res)))                {                    mysql_display(connection,sqlrow);                }                mysql_free_result(my_res);            }            else            {                mysql_err_function(connection);            }        }        mysql_close(connection);        cout << "Connection to MySQL Server is Closed!" << endl;    }    else    {        mysql_err_function(connection);    }}void mysql_err_function(MYSQL * connection){    if (mysql_errno(connection))    {        cout << "Error " << mysql_errno(connection) << " : "             << mysql_error(connection) << endl;        exit(-1);    }}void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow){    for (unsigned int i = 0; i < mysql_field_count(mysql); ++i)    {        printf("%s ",sqlrow[i]);        //cout << sqlrow[i] << &#39; &#39;;	//不知到为什么将printf换成cout之后,打印值就会出错...思考ing...    }    cout << endl;}
Copy after login

二、获取一个字段的信息

1、MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);  	2、MYSQL_FIELD定义:typedef struct st_mysql_field{    char *name;                        /* Name of column */    char *table;                        /* Table of column if column was a field */    char *org_table;                /* Org table name if table was an alias */    char *db;                        /* Database for table */    char *def;                        /* Default value (set by mysql_list_fields) */    unsigned long length;                /* Width of column */    unsigned long max_length;        /* Max width of selected set */    unsigned int flags;                /* Div flags */    unsigned int decimals;        /* Number of decimals in field */    enum enum_field_types type;        /* Type of field. Se mysql_com.h for types *
    
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template