linux c调用 mysql代码

 

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <time.h>
using namespace std;

MYSQL *g_conn; // mysql 连接
MYSQL_RES *g_res; // mysql 记录集
MYSQL_ROW g_row; // 字符串数组,mysql 记录行

#define MAX_BUF_SIZE 1024 // 缓冲区最大字节数

const char *g_host_name = "localhost";
const char *g_user_name = "root";
const char *g_password = "python123";
const char *g_db_name = "db_data_20170524164100";
const unsigned int g_db_port = 3306;

void print_mysql_error(const char *msg)   // 打印最后一次错误
{
    if (msg)
        printf("%s: %s\n", msg, mysql_error(g_conn));
    else
        puts(mysql_error(g_conn));
}

int executesql(const char * sql)
{
    /*query the database according the sql*/
    if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
        return -1; // 表示失败

    return 0; // 成功执行
}


int init_mysql()   // 初始化连接
{
    // init the database connection
    g_conn = mysql_init(NULL);

    /* connect the database */
    if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
        return -1;

    // 是否连接已经可用
    executesql("set names utf8"); // 如果失败
    // return -1;
    return 0; // 返回成功
}


int main(void)
{
    if (init_mysql());
    print_mysql_error(NULL);

    char sql[MAX_BUF_SIZE];

    int firstTime = time((time_t*)NULL);
    printf("firstTime is: %d\n", firstTime);

    if (executesql("select * from data_stats_gov_cn_diqu_niandu")) // 句末没有分号
        print_mysql_error(NULL);

    g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集

    int iNum_rows = mysql_num_rows(g_res); // 得到记录的行数
    int iNum_fields = mysql_num_fields(g_res); // 得到记录的列数

    printf("共%d个记录,每个记录%d字段\n", iNum_rows, iNum_fields);

    // puts("id\tname\n");

    while ((g_row=mysql_fetch_row(g_res))) ;// 打印结果集
    //        printf("%s\t%s\n", g_row[0], g_row[1]); // 第一,第二字段
    {
        for(int i=0; i<iNum_fields; i++)
        {
            printf("%s\t",g_row[i]);
        }
        printf("\n");
    }

    int secodnT
View Code

 

方法二:

这段代码链接:https://andrew913.iteye.com/blog/433280

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<mysql/mysql.h>  
#define MAX_COLUMN_LEN  32  
int main(int argc , char *argv[])  
{  
    MYSQL my_connection;  
    MYSQL_RES *result;  
    MYSQL_ROW sql_row;  
    MYSQL_FIELD *fd;  
    char column[MAX_COLUMN_LEN][MAX_COLUMN_LEN];  
    int res;  
    mysql_init(&my_connection);  
    if(mysql_real_connect(&my_connection,"127.0.0.1"  
                            ,"用户","密码","数据名称",3306,NULL,0))  
    {  
        perror("connect");  
        res=mysql_query(&my_connection,"select * from app");//查询  
        if(!res)  
        {  
            result=mysql_store_result(&my_connection);//保存查询到的数据到result  
            if(result)  
            {  
                int i,j;  
                printf("the result number is %lu\n ",(unsigned long)mysql_num_rows(result));  
                for(i=0;fd=mysql_fetch_field(result);i++)//获取列名  
                {  
                    bzero(column[i],sizeof(column[i]));  
                    strcpy(column[i],fd->name);  
                }  
                j=mysql_num_fields(result);  
                for(i=0;i<j;i++)  
                {  
                    printf("%s\t",column[i]);  
                }  
                printf("\n");  
                while(sql_row=mysql_fetch_row(result))//获取具体的数据  
                {  
                    for(i=0;i<j;i++)  
                    {  
                        printf("%s\t",sql_row[i]);  
                    }  
                    printf("\n");  
                }  
                  
            }  
        }  
        else  
        {  
            perror("select");  
        }  
    }  
    else  
    {  
        perror("connect:error");  
    }  
    mysql_free_result(MYSQL_RES *result);//释放结果资源  
    mysql_close(&my_connection);//断开连接  
  
}  
View Code

 

时间:

root@corleone:/opt/code/testC++/test/test5/test# ./mysql 

firstTime is: 1547606246
共1485729个记录,每个记录23字段
secodnTime is: 1547606249
remainTime is: 3

 

posted @ 2019-01-16 10:53  我当道士那儿些年  阅读(469)  评论(0编辑  收藏  举报