Home  >  Article  >  Database  >  Simple data query for getting started with MySQL

Simple data query for getting started with MySQL

黄舟
黄舟Original
2017-01-19 15:50:551440browse

mysql_init initializes the database link – gets a link mysql_real_connect connects to the database server and executes the mysql_query query – the query statement is a string that retrieves each row separately mysql_store_result – the results are stored in the link and are a one-time query. Get the table header from the result set Information – mysql_fetch_fields – header information is stored in the memory space pointed to by the MYSQL_FIELD type pointer. Parse the header – mysql_field_count to obtain the number of columns. The for loop parses column by column. mysql_fetch_row obtains data row by row from the result set and releases memory after parsing each column for each row. Space

Close link

Specific code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <mysql/mysql.h>
int main()
{
    int     ret = 0;
    int     i = 0;
    MYSQL   mysql;
    MYSQL   *con = NULL;
    unsigned int fieldnum;  
    printf("hello....\n");
    con = mysql_init(&mysql);
    if (con == NULL)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_init() err :%d\n", ret);
        return ret;
    }
    con = mysql_real_connect(&mysql, "localhost", "root", "123456", "mydb2", 0, NULL, 0 );
    if (con == NULL)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_real_connect() err :%d\n", ret);
        return ret;
    }
    else
    {
        printf("func mysql_real_connect() ok\n");
    }
    //执行sql查询
    mysql_query(&mysql, "set names utf8");//解决中文乱码
    char *sql = "select *from employee";
    ret = mysql_query(&mysql, sql);
    if (ret != 0)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_query() err :%d\n", ret);
        return ret;
    }
    /*
    //获取结果集 一次性获取
    MYSQL_RES * sqlres =mysql_store_result( &mysql);
    if (sqlres == NULL)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_store_result() err :%d\n", ret);
        return ret;
    }
    */
    //对每一行分别进行检索
    MYSQL_RES * sqlres =mysql_store_result(&mysql);
    if (sqlres == NULL)
    {
        ret = mysql_errno(&mysql);
        printf("func mysql_store_result() err :%d\n", ret);
        return ret;
    }
    //从结果集,获取表头信息
    MYSQL_FIELD *fields = mysql_fetch_fields(sqlres);
    fieldnum = mysql_field_count(&mysql);
    for (i=0; i<fieldnum; i++)
    {
        printf("%s\t", fields[i].name);
    }
    printf("\n");
    //从结果集, 按照行获取信息信息
    MYSQL_ROW row = NULL; //(char **)
    //从结果集中一行一行的获取数据
    while (  row = mysql_fetch_row(sqlres))
    {
        fieldnum = mysql_field_count(&mysql); 
        //优化,我的行有多少列。。。。查找这样的api函数
        for (i=0; i<fieldnum; i++) //经过测试 发现 不是以0结尾的指针数组。。
        {
            printf("%s\t", row[i]);
        }
        printf("\n");
    }
    mysql_free_result(sqlres);
    mysql_close(&mysql);
    return ret;
}

The above is the content of simple data query for getting started with MySQL. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!


Statement:
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