第二部分 : 通过普通用户完成业务需求.
先创建普通用户 csz, 密码是 1413222, 并并且给其 select 读权限.
su root mysql drop user csz; create user 'csz'@'%' identified by '13142222'; grant select on *.* to 'csz'@'%'; # 立即刷新 flush privileges;
后面登录试试
mysql -ucsz -p1314222 -h127.0.0.1

主要是mariadb默认关闭远程访问. 后面我们开启安全访问模式试试
su root /etc/init.d/mysql stop mysqld_safe --skip-grant-tables

后面再开启一个会话 . 重新输入 mysql -ucsz -p1314222 -h127.0.0.1 , 解决可以了

到这里 通过新建的 csz 用户访问成功. 好了. 那我们开始写一个 msyql c 代码处理我们的业务. 按照月来访问. 那我们开始写代码.代码的总的思路是拼接一个这样的串
#目标拼接 串内容 select sum(c) from ( select count(*) as c from 2016_6_1_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_2_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_3_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_4_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_5_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_9_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_10_oss_musicelves where specifytype=1 and char_id = 30422720614402293 union all select count(*) from 2016_6_12_oss_musicelves where specifytype=1 and char_id = 30422720614402293 ) as t;
具体看 getmouths.c 文件 内容
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <mysql.h> #define _INT_BUF (4098) // 得到查询数据表内容 #define _STR_SQLTABLES "select distinct table_name from information_schema.columns where table_name like '%d_%d_%%_oss_musicelves'" #define _STR_SQLSELECT "select count(*) as c from %s where specifytype=1 and char_id = %lld" // 基础的mariadb 错误关闭函数 static inline void _err_mariadb(MYSQL *con) { fprintf(stderr, "_err_mariadb error: %s\n", mysql_error(con)); mysql_close(con); exit(EXIT_FAILURE); } /* * 处理 oss_musicelves 一个月的所有表. */ int main(int argc, char* argv[]) { long long ptid; time_t rt = time(NULL); struct tm *pt = localtime(&rt); int year = pt->tm_year + 1900; int mouth = pt->tm_mon + 1; // 先简单检测输入 if(argc <= 1) { fprintf(stderr, "%s [ptid] [mouth] [year]\n", argv[0]); exit(EXIT_FAILURE); } // 先得到 ptid 数据 ptid = atoll(argv[1]); // 得到当前月份 if(argc >= 3) mouth = atoi(argv[2]); // 得到当前年份 if(argc >= 4) year = atoi(argv[3]); // 简单检测结果是否合法 if(ptid < 0 || mouth <=0 || mouth>12 || year<1900) { fprintf(stderr, "%s %lld %d %d is error!\n", argv[0], ptid, mouth, year); exit(EXIT_FAILURE); } // 输出结果 printf("%s %lld %d %d start run!\n", argv[0], ptid, mouth, year); // 开始用mysql 访问我们需要访问的数据结果了 MYSQL *con = mysql_init(NULL); if(con == NULL) { fprintf(stderr, "mysql_init error: %s\n", mysql_error(con)); exit(EXIT_FAILURE); } if(!mysql_real_connect(con, "127.0.0.1", "csz", "1314222", "oss_log", 0, NULL, 0)) _err_mariadb(con); char sqls[_INT_BUF]; int sqlen = 0; sprintf(sqls, _STR_SQLTABLES, year, mouth); if(mysql_query(con, sqls)) _err_mariadb(con); // 开始得到结果 MYSQL_RES *ret = mysql_store_result(con); if(NULL == ret) _err_mariadb(con); MYSQL_ROW row; int i = 0, nr = 0; while(!!(row = mysql_fetch_row(ret))) { if(i == 0) { sqlen = sprintf(sqls, "select sum(c) from (\n" _STR_SQLSELECT, row[0], ptid); i = 1; continue; } // 后面正常拼接 nr = sprintf(sqls + sqlen, "\nunion all\n" _STR_SQLSELECT, row[0], ptid); if((sqlen += nr) >= _INT_BUF) { fprintf(stderr, "sprintf while %d too length.\n", sqlen); goto __return_free; } } if(i == 0) { printf("sum %lld %d/%d: 0\n", ptid, year, mouth); goto __return_free; } // 这里处理有的数据 nr = sprintf(sqls + sqlen, "\n) as t;"); if((sqlen += nr) >= _INT_BUF) { fprintf(stderr, "sprintf end %d too length.\n", sqlen); goto __return_free; } // 内存用完了就直接释放 mysql_free_result(ret); ret = NULL; printf("sql : \n\t%s\n", sqls); // 开始输出统计结果 if(mysql_query(con, sqls)) _err_mariadb(con); if((ret = mysql_store_result(con))==NULL) _err_mariadb(con); //得到结果直接返回 if(!!(row=mysql_fetch_row(ret))) printf("sum %lld %d/%d: %s\n", ptid, year, mouth, row[0]); else puts("select is empty!"); __return_free: // 释放用过的内存 mysql_free_result(ret); // 关闭打开的 数据库访问对象 mysql_close(con); return 0; }
编译命令
gcc -Wall -ggdb2 -I/usr/include/mariadb -o getmouths.out getmouths.c -lmysqlclient
最终运行结果是

如果想详细了解关于mariadb c驱动的api使用, 可以参照老外写的很好理解.
mysqlc demo http://zetcode.com/db/mysqlc/
到这里就结束了, 关于C 访问数据库能力也基本打通了.
MariaDB 的详细介绍:请点这里
MariaDB 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-06/132206.htm
