mysql client api 的封装

前言

这几天,在测试程序中要用mysql client api 去数据库插入记录和查询记录。
好久不用mysql client api, 没有封装好的存货。只能用api直接干活。
前天闲下来了,将mysql client api的使用封装成类,对执行sql语句的操作封装了成员函数,实现库的建立,表的建立,use database, 记录的增删改查. 等下次再用到,就直接用这个类干活了。

实验环境

debian8.8 + mysql5.5.60-0+deb8u1

前置任务

如果连接的不是本地mysql数据库,需要在数据库改用户权限,使远程访问数据库成为可能。
这活,如果是在工作中,已经由运维人员搞好了。
如果是自己做实验,这步要自己做。

# 在debian8.8中安装mysql 
aptitude install mysql-server mysql-client

# 登陆mysql客户端 ,连接本地数据库
mysql -h 127.0.0.1 -u root -p 

# 如果不是回环ip访问, 需要在配置文件中绑定允许访问的客户端ip
vi /etc/mysql/my.cnf 
# bind-address          = 127.0.0.1 
# ref https://dev.mysql.com/doc/refman/8.0/en/server-options.html 
bind-address            = * 

# 重启mysql服务 
/etc/init.d/mysql restart 

# 修改mysql配置之后, 就可以用回环地址或者本地IP地址登陆msyql客户端工具了
mysql -h 192.168.11.140 -u root -p

建立供远程访问的mysql用户

mysql> select host,user,password from mysql.user;

mysql口令的用户长度是定长的
mysql> create user remote_user identified by password '12345678901234567890123456789012345678901'; 
Query OK, 0 rows affected (0.00 sec) 

给刚建立远程用户设置所有数据库操作的权限.设置完后,就可以用本地活远程用服务器的IP来登陆mysql了
实际的工作数据库,运维人员都设置好了,我们不管.
grant all privileges on *.* to remote_user identified by '12345678901234567890123456789012345678901';

其他一些可能用到的mysql客户端的命令. 其实到了这步,就可以在windows上搞个图形化的数据库工具来做实验了.
mysql> SHOW DATABASES;   //显示数据库
mysql> USE my_db         //进入数据库
mysql> SHOW TABLES;      //显示表
mysql> DESCRIBE mytable; //显示表结构
mysql> CREATE DATABASE abccs;    //创建一个数据库
mysql> select version();  // 显示数据库版本

工程下载

src_test_mysql.7z

工程运行效果

main.cpp.31 : main()] : MAKE_FILE_MACRO__BIN_NAME = [test_mysql]
main.cpp.71 : fn_test()] : >> fn_test()
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'my_test_db';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(is_db_exist)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 1, col = 1
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [my_test_db]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(USE my_test_db;)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'my_test_db' AND table_name = 'tbl_app_data_index_files' LIMIT 1;)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(is_tbl_exist)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 1, col = 1
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_1.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_1)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_2.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_2)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_3.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_3)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_4.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_4)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_1.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_5)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_2.dat');)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_ADD_6)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '01';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_1)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 4, col = 4
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [85]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.3] = [/var/log/test1_1.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.0] = [86]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.3] = [/var/log/test1_2.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.0] = [87]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.3] = [/var/log/test1_3.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.0] = [88]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.2] = [01]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.3] = [/var/log/test1_4.dat]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(delete from tbl_app_data_index_files where app_id = '01';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_DEL_1)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '01';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_1)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 0, col = 4
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[-1.-1] = [NULL]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '02';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_2)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 30, col = 4
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [5]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.0] = [6]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.0] = [11]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.0] = [12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.0] = [17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.0] = [18]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.0] = [23]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.0] = [24]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.0] = [29]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.1] = [2018-11-06 14:14:47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.0] = [30]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.1] = [2018-11-06 14:14:48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.0] = [35]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.1] = [2018-11-06 14:23:12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.0] = [36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.1] = [2018-11-06 14:23:13]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.0] = [41]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.1] = [2018-11-06 14:29:14]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.0] = [42]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.1] = [2018-11-06 14:29:27]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.0] = [47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.1] = [2018-11-06 14:33:17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.0] = [48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.1] = [2018-11-06 14:33:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.0] = [53]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.1] = [2018-11-06 14:52:44]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.0] = [54]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.1] = [2018-11-06 14:52:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.0] = [59]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.0] = [60]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.0] = [65]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.0] = [66]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.0] = [71]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.0] = [72]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.0] = [77]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.0] = [78]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.0] = [83]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.1] = [2018-11-06 15:28:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.0] = [84]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.1] = [2018-11-06 15:28:20]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.0] = [89]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.3] = [/var/log/test2_1.dat]
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.0] = [90]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.3] = [/var/log/test2_2.dat]
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(update tbl_app_data_index_files set file_path_name = '' where app_id = '02';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_MODIFY)
db_opt_mysql.cpp.381 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.382 : exec_sql()] : # exec_sql(select * from tbl_app_data_index_files where app_id = '02';)
db_opt_mysql.cpp.383 : exec_sql()] : # --------------------------------------------------------------------------------
db_opt_mysql.cpp.391 : exec_sql()] : ok : exec_sql
db_opt_mysql.cpp.292 : process_result_set()] : cls_db_opt_mysql::process_result_set(SQL_QUERY_2)
db_opt_mysql.cpp.309 : process_result_set()] : result set : row = 30, col = 4
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.0] = [5]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[0.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.0] = [6]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.1] = [2018-11-06 10:16:31]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[1.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.0] = [11]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[2.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.0] = [12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.1] = [2018-11-06 13:06:34]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[3.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.0] = [17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[4.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.0] = [18]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.1] = [2018-11-06 13:07:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[5.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.0] = [23]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[6.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.0] = [24]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.1] = [2018-11-06 13:09:36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[7.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.0] = [29]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.1] = [2018-11-06 14:14:47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[8.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.0] = [30]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.1] = [2018-11-06 14:14:48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[9.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.0] = [35]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.1] = [2018-11-06 14:23:12]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[10.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.0] = [36]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.1] = [2018-11-06 14:23:13]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[11.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.0] = [41]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.1] = [2018-11-06 14:29:14]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[12.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.0] = [42]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.1] = [2018-11-06 14:29:27]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[13.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.0] = [47]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.1] = [2018-11-06 14:33:17]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[14.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.0] = [48]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.1] = [2018-11-06 14:33:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[15.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.0] = [53]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.1] = [2018-11-06 14:52:44]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[16.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.0] = [54]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.1] = [2018-11-06 14:52:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[17.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.0] = [59]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[18.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.0] = [60]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.1] = [2018-11-06 15:04:49]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[19.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.0] = [65]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[20.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.0] = [66]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.1] = [2018-11-06 15:05:09]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[21.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.0] = [71]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[22.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.0] = [72]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.1] = [2018-11-06 15:07:56]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[23.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.0] = [77]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[24.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.0] = [78]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.1] = [2018-11-06 15:14:38]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[25.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.0] = [83]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.1] = [2018-11-06 15:28:19]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[26.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.0] = [84]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.1] = [2018-11-06 15:28:20]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[27.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.0] = [89]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[28.3] = []
db_opt_mysql.cpp.323 : process_result_set()] : --------------------------------------------------------------------------------
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.0] = [90]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.1] = [2018-11-06 15:34:00]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.2] = [02]
db_opt_mysql.cpp.342 : cb_proc_resultset_to_disp()] : col[29.3] = []
main.cpp.75 : fn_test()] : test_db_mysql ok
main.cpp.39 : main()] : THE END

工程预览

// @file \test_mysql\src\db_business_logic.h

#ifndef __DB_BUSINESS_LOGIC_H__
#define __DB_BUSINESS_LOGIC_H__

bool test_db_mysql();

#endif // #ifndef __DB_BUSINESS_LOGIC_H__


// @file \test_mysql\src\db_business_logic.cpp

#include "db_business_logic.h"

#include "db_opt_mysql.h"
#include "const_define.h"

#define APP_ID "01"
#define APP_DATA_FILE_PATH_NAME "/var/log/test1.dat"

#define DB_IP "192.168.16.44"
#define DB_PORT 3306
#define DB_NAME "my_test_db"
#define DB_CHARSET "utf8" // 是utf8, 不是UTF-8 !!
#define DB_USER_NAME "root"
#define DB_USER_PWD "123456"
#define DB_TBL_NAME "tbl_app_data_index_files"

// 增
#define SQL_ADD_1 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_1.dat');"
#define SQL_ADD_2 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_2.dat');"
#define SQL_ADD_3 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_3.dat');"
#define SQL_ADD_4 "insert into tbl_app_data_index_files(app_id, file_path_name)values('01', '/var/log/test1_4.dat');"
#define SQL_ADD_5 "insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_1.dat');"
#define SQL_ADD_6 "insert into tbl_app_data_index_files(app_id, file_path_name)values('02', '/var/log/test2_2.dat');"

// 删
#define SQL_DEL_1 "delete from tbl_app_data_index_files where app_id = '01';"

// 查 - for SQL_DEL_1
#define SQL_QUERY_1 "select * from tbl_app_data_index_files where app_id = '01';"

// 改
#define SQL_MODIFY "update tbl_app_data_index_files set file_path_name = '' where app_id = '02';"

// 查  - for SQL_MODIFY
#define SQL_QUERY_2 "select * from tbl_app_data_index_files where app_id = '02';"

bool test_db_mysql()
{
	bool b_rc = false;
	cls_db_opt_mysql db_opt;

	do {
		db_opt.set_db_connect_info(
			DB_IP, 
			DB_PORT, 
			DB_NAME, 
			DB_CHARSET, 
			DB_USER_NAME, 
			DB_USER_PWD,
			DB_TBL_NAME);

		if (!db_opt.connect_server(true)) {
			break;
		}
		
		if (!db_opt.create_db()) {
			break;
		}

		if (!db_opt.use_db()) {
			break;
		}

		if (!db_opt.create_tbl()) {
			break;
		}

		// SQL-增
		if (!db_opt.exec_sql(SQL_ADD_1)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_2)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_2", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_3)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_3", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_4)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_4", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_5)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_5", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		if (!db_opt.exec_sql(SQL_ADD_6)) {
			break;
		}
		db_opt.process_result_set("SQL_ADD_6", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// SQL-删 + 查
		// query before del
		if (!db_opt.exec_sql(SQL_QUERY_1)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// sql delete
		if (!db_opt.exec_sql(SQL_DEL_1)) {
			break;
		}
		db_opt.process_result_set("SQL_DEL_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// query after delete
		if (!db_opt.exec_sql(SQL_QUERY_1)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_1", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// SQL-改 + 查
		// query befor modify
		if (!db_opt.exec_sql(SQL_QUERY_2)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_2", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// sql modify
		if (!db_opt.exec_sql(SQL_MODIFY)) {
			break;
		}
		db_opt.process_result_set("SQL_MODIFY", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		// query after modify
		if (!db_opt.exec_sql(SQL_QUERY_2)) {
			break;
		}
		db_opt.process_result_set("SQL_QUERY_2", &cls_db_opt_mysql::cb_proc_resultset_to_disp);

		b_rc = true;
	} while (0);

	db_opt.db_close();
	return b_rc;
}


// @file \test_mysql\src\db_opt_base.h

#ifndef __DB_OPT_BASE_H__
#define __DB_OPT_BASE_H__

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>

class cls_db_opt_base
{
public:
	cls_db_opt_base();
	virtual ~cls_db_opt_base();

	void set_db_connect_info(
		const char* ip, 
		int port,
		const char* db_name, 
		const char* psz_charset,
		const char* user_name, 
		const char* user_pwd,
		const char* tbl_name);

	virtual bool connect_server(bool b_force) = 0; // 连接服务器是否成功

	virtual bool is_db_exist() = 0; // 数据库是否存在
	
	virtual bool create_db() = 0; // 建立数据库
	virtual bool use_db() = 0; // 切换到指定的数据库
	virtual bool is_tbl_exist() = 0; // 表是否存在
	virtual bool create_tbl() = 0; // 建立表
	virtual bool exec_sql(const char* psz_sql) = 0; // 执行SQL语句
	virtual bool db_close() = 0; // 关闭数据库
	virtual std::string get_err_string() = 0; // 取当前错误字符串
	virtual uint get_err_sn() = 0; // 取当前错误号

	const char* get_db_name() {return m_str_db_name.c_str();}
	const char* get_charset() {return m_str_charset.c_str();}
	const char* get_tbl_name() {return m_str_tbl_name.c_str();}

protected:
	bool m_is_mysql_was_init; // 是否初始化了mysql
	bool m_is_server_was_connected; // 是否已经连接到了服务器
	
	bool m_is_db_was_exist; // 数据库是否存在
	bool m_is_db_was_create; // 是否已经建立了数据库
	bool m_is_db_was_switched; // 是否已经切换到了参数指定的数据库
	
	bool m_is_tbl_exist; // 表是否存在
	bool m_is_tbl_was_create; // 表是否已经创建

	std::string m_str_ip;
	int m_i_db_port;
	std::string m_str_db_name;
	std::string m_str_charset;
	
	std::string m_str_user_name;
	std::string m_str_user_pwd;

	std::string m_str_tbl_name;
};

#endif // #ifndef __DB_OPT_BASE_H__


// @file \test_mysql\src\db_opt_base.cpp

#include "db_opt_base.h"

cls_db_opt_base::cls_db_opt_base()
{
	m_is_mysql_was_init = false;
	m_is_server_was_connected = false;
	
	m_is_db_was_exist = false;
	m_is_db_was_create = false;
	m_is_db_was_switched = false;

	m_str_ip = "";
	m_i_db_port = 0;
	m_str_db_name = "";
	m_str_charset = "";
	
	m_str_user_name = "";
	m_str_user_pwd = "";

	m_str_tbl_name = "";
}

cls_db_opt_base::~cls_db_opt_base()
{
}

void cls_db_opt_base::set_db_connect_info(
	const char* ip, 
	int port,
	const char* db_name, 
	const char* psz_charset,
	const char* user_name, 
	const char* user_pwd,
	const char* tbl_name)
{
	m_str_ip = (NULL != ip) ? ip : "";
	m_i_db_port = port;
	m_str_db_name = (NULL != db_name) ? db_name : "";
	m_str_charset = (NULL != psz_charset) ? psz_charset : "";
	
	m_str_user_name = (NULL != user_name) ? user_name : "";
	m_str_user_pwd = (NULL != user_pwd) ? user_pwd : "";

	m_str_tbl_name = (NULL != tbl_name) ? tbl_name : "";
}


// @file \test_mysql\src\db_opt_mysql.h

#ifndef __DB_OPT_MYSQL_H__
#define __DB_OPT_MYSQL_H__

#include "db_opt_base.h"
#include "mysql.h"

class cls_db_opt_mysql : public cls_db_opt_base
{
public:
	typedef bool (cls_db_opt_mysql::*PFN_cb_proc_resultset)(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

public:
	cls_db_opt_mysql();
	virtual ~cls_db_opt_mysql();

	bool process_result_set(const char* psz_tip, PFN_cb_proc_resultset cb);
	bool cb_proc_resultset_to_disp(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

	virtual bool connect_server(bool b_force);

	virtual bool is_db_exist();
	bool cb_proc_resultset_for_is_db_exist(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

	virtual bool create_db();
	virtual bool use_db();
	
	virtual bool is_tbl_exist();
	bool cb_proc_resultset_for_is_tbl_exist(
		int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok);

	virtual bool create_tbl();
	virtual bool exec_sql(const char* psz_sql);
	virtual bool db_close();

	virtual std::string get_err_string();
	virtual uint get_err_sn();

	MYSQL* get_db_handle() {return     &m_h_mysql;}

private:
	MYSQL m_h_mysql; // mysql库的句柄
};

#endif // #ifndef __DB_OPT_MYSQL_H__


// @file \test_mysql\src\db_opt_mysql.cpp

#include "db_opt_mysql.h"
#include "const_define.h"

cls_db_opt_mysql::cls_db_opt_mysql()
{
	memset(&m_h_mysql, 0, sizeof(MYSQL));
}

cls_db_opt_mysql::~cls_db_opt_mysql()
{
}

bool cls_db_opt_mysql::connect_server(bool b_force)
{
	do {
		if (b_force) {
			db_close();
		}

		if (!m_is_mysql_was_init) {
		    if (mysql_init(&m_h_mysql) == NULL) {
				MYLOG_E("error : %s\n", get_err_string().c_str());
		        break;
		    }

			m_is_mysql_was_init = true;
		}

		if (!m_is_server_was_connected) {
		    if (NULL == mysql_real_connect(&m_h_mysql, 
					m_str_ip.c_str(), 
					m_str_user_name.c_str(), 
					m_str_user_pwd.c_str(), 
					NULL, 
					m_i_db_port, 
					NULL, 
					0))
			{
				MYLOG_E("error : %s\n", get_err_string().c_str());
		        break;
		    }

			m_is_server_was_connected = true;
		}
	} while (0);

	return m_is_server_was_connected;
}

bool cls_db_opt_mysql::is_db_exist()
{
	// SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA;
	// SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'my_test_db';

	bool b_rc = false;
	char sz_buf[1024] = {'\0'};

	do {
		if (!m_is_db_was_exist) {
			if (!m_is_server_was_connected) {
				b_rc = connect_server(false);
				if (!b_rc) {
					break;
				}
			}

			sprintf(sz_buf, 
				"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '%s';",
				get_db_name());
			
			b_rc = exec_sql(sz_buf);
			if (!b_rc) {
				break;
			}

			m_is_db_was_exist = process_result_set("is_db_exist", &cls_db_opt_mysql::cb_proc_resultset_for_is_db_exist);
		}
	} while (0);

	return m_is_db_was_exist;
}

bool cls_db_opt_mysql::cb_proc_resultset_for_is_db_exist(
	int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok)
{
	if (NULL != b_process_ok) {
		*b_process_ok = false;
	}

	cb_proc_resultset_to_disp(i_rows, i_cols, i_row_index, i_col_index, psz_grid_content, NULL);

	if ((1 == i_rows) 
		&& (1 == i_cols) 
		&& (0 == i_row_index) 
		&& (0 == i_col_index) 
		&& (NULL != psz_grid_content))
	{
		// 一行一列, 且格点内容为数据库名称
		if (0 == strcmp(get_db_name(), psz_grid_content)) {
			if (NULL != b_process_ok) {
				*b_process_ok = true;
			}
		}
	}
	
	return false; // 数据库是否存在的判断, 只处理一次
}

bool cls_db_opt_mysql::create_db()
{
	bool b_rc = false;
	char sz_buf[1024] = {'\0'};

	do {
		b_rc = is_db_exist();
		if (b_rc) {
			m_is_db_was_create = true;
			break;
		}

		sprintf(sz_buf, 
			"create database %s;",
			get_db_name());
		
		b_rc = exec_sql(sz_buf);
		if (!b_rc) {
			break;
		}

		// 建立数据库的SQL是没有结果集的
		m_is_db_was_create = true;
	} while (0);

	return b_rc;
}

bool cls_db_opt_mysql::use_db()
{
	char sz_buf[1024] = {'\0'};

	do {
		if (!connect_server(false)) {
			break;
		}

		if (!m_is_db_was_switched) {
			// USE database_name;
			sprintf(sz_buf, 
				"USE %s;",
				get_db_name());
			
			if (!exec_sql(sz_buf)) {
				break;
			}

			// 切换数据库是没有结果集的
			m_is_db_was_switched = true;

			// 切换到了具体的数据库, 才能设置字符集
			if (mysql_set_character_set(&m_h_mysql, get_charset()) != 0) {
				// 字符集设置错了,还要继续, 不能算错
				MYLOG_E("error : %s\n", get_err_string().c_str());
			}
		}
	} while (0);

	return m_is_db_was_switched;
}

bool cls_db_opt_mysql::cb_proc_resultset_for_is_tbl_exist(
	int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok)
{
	if (NULL != b_process_ok) {
		*b_process_ok = false;
	}

	if ((1 == i_rows) 
		&& (1 == i_cols) 
		&& (0 == i_row_index) 
		&& (0 == i_col_index) 
		&& (NULL != psz_grid_content))
	{
		// 一行一列, 且格点内容为数据库名称
		if (0 == strcmp(get_tbl_name(), psz_grid_content)) {
			if (NULL != b_process_ok) {
				*b_process_ok = true;
			}
		}
	}
	
	return false; // 表是否存在的判断, 只处理一次
}

bool cls_db_opt_mysql::is_tbl_exist()
{
	char sz_buf[1024] = {'\0'};

	do {
		if (!m_is_tbl_exist) {
			if (!use_db()) {
				break;
			}

			/*
			SELECT TABLE_NAME FROM information_schema.tables 
			WHERE table_schema = 'my_test_db' AND table_name = 'tbl_app_data_index_files';
			*/
			
			sprintf(sz_buf, 
				"SELECT TABLE_NAME "
				"FROM information_schema.tables "
				"WHERE table_schema = '%s' "
			    "AND table_name = '%s' "
				"LIMIT 1;",
				get_db_name(),
				get_tbl_name());
			
			if (!exec_sql(sz_buf)) {
				break;
			}

			m_is_tbl_exist = process_result_set("is_tbl_exist", &cls_db_opt_mysql::cb_proc_resultset_for_is_tbl_exist);
		}
	} while (0);

	return m_is_tbl_exist;
}

bool cls_db_opt_mysql::create_tbl()
{
	char sz_buf[1024] = {'\0'};

	do {
		if (!m_is_tbl_was_create) {
			if (is_tbl_exist()) {
				m_is_tbl_was_create = true;
				break;
			}

			/**
			CREATE TABLE my_test_db.tbl_app_data_index_files (
				`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				`time_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
				`app_id` VARCHAR(64) NOT NULL,
				`file_path_name` varchar(1024) NOT NULL,
				PRIMARY KEY (`id`)
				) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
			*/

			// CREATE TABLE IF NOT EXISTS
			// 这种语法不行(IF NOT EXISTS)
			sprintf(sz_buf, 
				"CREATE TABLE %s.%s ("
				"`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,"
				"`time_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,"
				"`app_id` VARCHAR(64) NOT NULL,"
				"`file_path_name` varchar(1024) NOT NULL,"
				"PRIMARY KEY (`id`)"
				") ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=%s;",
				get_db_name(),
				get_tbl_name(),
				get_charset());
			
			if (!exec_sql(sz_buf)) {
				break;
			}

			// 建立表的SQL没有结果集
			m_is_tbl_was_create = true;
		}
	} while (0);

	return m_is_tbl_was_create;
}

bool cls_db_opt_mysql::process_result_set(const char* psz_tip, PFN_cb_proc_resultset cb)
{
	bool b_process_ok = false;
	bool b_break_loop = false;
	
	MYSQL_RES* db_result = NULL;
	MYSQL_ROW db_row;
	uint i_num_fields = 0;
	ull ull_rows = 0;
	ull i = 0;
	ull j = 0;

	do {
		if (NULL != psz_tip) {
			MYLOG_D("cls_db_opt_mysql::process_result_set(%s)\n", psz_tip);
		}
		
		if (NULL == cb) {
			b_process_ok = true;
			break;
		}
		
		db_result = mysql_store_result(get_db_handle());
		if (NULL == db_result) {
			b_process_ok = false;
			break;
		}
		
		ull_rows = mysql_num_rows(db_result);
		i_num_fields = mysql_num_fields(db_result);

		MYLOG_D("result set : row = %d, col = %d\n", (int)ull_rows, (int)i_num_fields);
		if (ull_rows <= 0) {
			if (!(this->*cb)((int)ull_rows, (int)i_num_fields, -1, -1, NULL, &b_process_ok)) {
				break;
			}
		}
		
		do {
			db_row = mysql_fetch_row(db_result);
			if (NULL == db_row) {
				break;
			}

			if (NULL != cb) {
				MYLOG_D("%s\n", LINE80);
				for (j = 0; j < i_num_fields; j++) {
					if (!(this->*cb)((int)ull_rows, (int)i_num_fields, (int)i, (int)j, db_row[j], &b_process_ok)) {
						b_break_loop = true;
						break;
					}
				}
			}
			
			i++;
		} while (!b_break_loop);
	} while (0);

	return b_process_ok;
}

bool cls_db_opt_mysql::cb_proc_resultset_to_disp(
	int i_rows, int i_cols, int i_row_index, int i_col_index, const char* psz_grid_content, bool* b_process_ok)
{
	MYLOG_D("col[%d.%d] = [%s]", i_row_index, i_col_index, (NULL != psz_grid_content) ? psz_grid_content : "NULL");

	if (NULL != b_process_ok) {
		*b_process_ok = true; // 处理成功
	}
	
	return true; // 继续处理下一个回调
}

bool cls_db_opt_mysql::db_close()
{
	if (m_is_mysql_was_init) {
		mysql_close(&m_h_mysql);
	}

	m_is_mysql_was_init = false;
	m_is_server_was_connected = false;

	m_is_db_was_exist = false;
	m_is_db_was_create = false;
	m_is_db_was_switched = false;

	m_is_tbl_exist = false;
	m_is_tbl_was_create = false;

	return true;
}

bool cls_db_opt_mysql::exec_sql(const char* psz_sql)
{
	int i_rc = -1;
	int failed_count = 0;
	std::string str = "";
	
	do {
		if (NULL == psz_sql) {
			break;
		}

		MYLOG_D("# %s\n", LINE80);
		MYLOG_D("# exec_sql(%s)\n", psz_sql);
		MYLOG_D("# %s\n", LINE80);
		do {
			// 当执行 delete from tbl_app_data_index_files where app_id = '01';
			// 第一次会报错如下:
			// Commands out of sync; you can't run this command now
			// 重连数据库 + use db后成功
			i_rc = mysql_query(&m_h_mysql, psz_sql);
			if (0 == i_rc) {
				MYLOG_D("ok : exec_sql\n");
				break;
			} else {
				MYLOG_E("error : %s\n", get_err_string().c_str());
				connect_server(true);
				use_db();
			}

			// 失败重试, 假设是sql没写错, 首次执行SQL, 没连接数据库引起的
			// 如果是SQL写错了, 那就没折了
		} while (failed_count++ < 1);
	} while (0);

	return (0 == i_rc);
}

std::string cls_db_opt_mysql::get_err_string()
{
	return mysql_error(&m_h_mysql);
}

uint cls_db_opt_mysql::get_err_sn()
{
	return mysql_errno(&m_h_mysql);
}


// @file main.cpp
// @brief 对mysql操作进行封装, 使操作mysql数据库方便些
// @note 
//		实验环境:
//				debian8.8

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <string>
#include <errno.h>

#include "const_define.h"
#include "db_business_logic.h"

void init(const char* psz_log_owner_name);
void uninit();
void proc_sig_term(int num);
int fn_test(int argc, char** argv);

int main(int argc, char** argv)
{
    char sz_buf[MAX_MSG_LENGTH] = {'\0'};

#ifdef MAKE_FILE_MACRO__BIN_NAME
    sprintf(sz_buf, "%s", MAKE_FILE_MACRO__BIN_NAME);
    init(sz_buf);
    MYLOG_D("MAKE_FILE_MACRO__BIN_NAME = [%s]\n", MAKE_FILE_MACRO__BIN_NAME);
#else
    init(NULL);
#endif // #ifdef MAKE_FILE_MACRO__BIN_NAME

    fn_test(argc, argv);
    uninit();

    MYLOG_D("THE END\n");
    return EXIT_SUCCESS;
}

void uninit()
{
}

void proc_sig_term(int num)
{
    MYLOG_D("SIGTERM = %d, num = %d\n", SIGTERM, num);
    MYLOG_D("maybe can do some clean task before quit\n");
    exit(1);	
}

void init(const char* psz_log_owner_name)
{
    int i = 0;

    // daemon(0, 0);

    // clear screen (print 25 empty line)
    for (i = 0; i < 25; i++) {
        MYLOG_D("\n");
    }

    signal(SIGTERM, proc_sig_term);
}

int fn_test(int argc, char** argv)
{
	bool b_rc = false;
    MYLOG_D(">> fn_test()\n");

	do {
		b_rc = test_db_mysql();
		MYLOG_D("test_db_mysql %s\n", b_rc ? "ok" : "failed");
		if (!b_rc) {
			break;
		}
	
		b_rc = true;
	} while (0);

	if (!b_rc) {
		MYLOG_D("test failed r\n");
	}

    return 0;
}


// @file const_define.h

#if not defined(__CONST_DEFINE_H__)
#define __CONST_DEFINE_H__

#include <string.h>
#include <stdint.h>
#include <syslog.h>

#include <string>
#include <list>

typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned long long ull;

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
    if (NULL != (p)) { \
        delete (p); \
        (p) = NULL; \
    }
#endif // #ifndef SAFE_DELETE

#ifndef SAFE_DELETE_ARY
#define SAFE_DELETE_ARY(p) \
    if (NULL != (p)) { \
        delete[] (p); \
        (p) = NULL; \
    }
#endif // #ifndef SAFE_DELETE

#define TITLE_LINE80 "================================================================================"
#define LINE80 "--------------------------------------------------------------------------------"

#if not defined(MYLOG_D)

#define USE_SYSLOG

#ifdef USE_SYSLOG
#define MYLOG_V(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_V", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_D(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_D", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_I(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_I", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_W(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_W", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_E(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_E", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);

#define MYLOG_F(fmt, ...) \
	syslog(LOG_INFO, "[%s : %s.%d : %s()] : " fmt, "MYLOG_F", __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);
#else // #ifdef USE_SYSLOG
#define MYLOG_V printf
#define MYLOG_D printf
#define MYLOG_I printf
#define MYLOG_W printf
#define MYLOG_E printf
#define MYLOG_F printf
#endif // #ifdef USE_SYSLOG

#endif // #if not defined(MYLOG_D)

#define MAX_SQL_LENGTH (1024 * 8)
#define MAX_MSG_LENGTH (1024 * 4)

#endif // #if not defined(__CONST_DEFINE_H__)


# ==============================================================================
# @file makefile
# ==============================================================================
# @note 
# howto build project
# 		make BIN_NAME="bin_name_by_you_want" rebuild
# makefile code need tab key not sapce key

MY_MAKE_FILE_PATH_NAME = $(MAKEFILE_LIST)

# macro from Makefile command line
# BIN_NAME

# macro to C project
MAKE_FILE_MACRO__BIN_NAME="make_file_macro__bin_name"

# var define on Makefile
BIN = output_not_give_bin_name
IS_BUILD_TYPE_VALID = 0

ifdef BIN_NAME
	IS_BUILD_TYPE_VALID = 1
	BIN = $(BIN_NAME)
	MAKE_FILE_MACRO__BIN_NAME=$(BIN_NAME)
else
	IS_BUILD_TYPE_VALID = 0
endif

LINE80 = --------------------------------------------------------------------------------

# CC = g++ -std=c++98
CC = g++

# -Werror is "warning as error"
CFLAGS = -Wall -Werror -g

INC = -I. -I../doc/mysql/include/
LIBPATH = -L/usr/lib/ -L/usr/local/lib/ -L../doc/mysql/lib/

ifeq (1, $(IS_BUILD_TYPE_VALID))
	LIBS = -lstdc++ -pthread -lmysqlclient
else
	LIBS =
endif

DEPEND_CODE_DIR = ../common/ \

DEPEND_CODE_SRC = $(shell find $(DEPEND_CODE_DIR) -name '*.cpp')
DEPEND_CODE_OBJ = $(DEPEND_CODE_SRC:.cpp=.o)

ROOT_CODE_SRC = $(shell find ./ -name '*.cpp')
ROOT_CODE_OBJ = $(ROOT_CODE_SRC:.cpp=.o)

SUB_CODE_DIR = ./empty_dir
SUB_CODE_SRC = $(shell find $(SUB_CODE_DIR) -name '*.cpp')
SUB_CODE_OBJ = $(SUB_CODE_SRC:.cpp=.o)

.PHONY: help
help:
	clear
	@echo "usage:"
	@echo
	@echo "build project by given bin name"
	@echo "make BIN_NAME=\"bin_name_by_you_want\" rebuild"
	@echo

.PHONY: clean
clean:
	clear

	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo

	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo
	@echo

	@echo
	@echo
	@echo
	@echo
	@echo

	@echo "make clean begin"
	@echo $(LINE80)

	@echo "@file $(MY_MAKE_FILE_PATH_NAME)"
	@echo "IS_BUILD_TYPE_VALID = $(IS_BUILD_TYPE_VALID)"
	@echo "BIN = $(BIN)"

	@echo $(LINE80)

	rm -f $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)

ifeq (1, $(IS_BUILD_TYPE_VALID))
	rm -f ./$(BIN)
endif

	@echo "make clean over"

.PHONY: all
all:$(BIN)
	@echo $(LINE80)
	@echo make all
	chmod 777 ./$(BIN)
	find . -name "$(BIN)"

$(BIN) : $(ROOT_CODE_OBJ) $(DEPEND_CODE_OBJ) $(SUB_CODE_OBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(SHLIBS) $(INC) $(LIBPATH) $(LIBS)

.cpp.o:
	$(CC) -c $(CFLAGS) -DMAKE_FILE_MACRO__BIN_NAME="\"$(MAKE_FILE_MACRO__BIN_NAME)\"" $^ -o $@ $(INC) $(LIBPATH) $(LIBS)

.PHONY: rebuild
rebuild:
	make -f $(MY_MAKE_FILE_PATH_NAME) clean

ifeq (1, $(IS_BUILD_TYPE_VALID))
	@echo $(LINE80)
	make -f $(MY_MAKE_FILE_PATH_NAME) all
	chmod 775 ./$(BIN)
	ldd ./$(BIN)
else
	@echo $(LINE80)
	@echo "error : Makefile command line input error, please see help"	
	@echo "please run => make help"	
	@echo $(LINE80)
endif


#!/bin/bash
# ==============================================================================
# @file build_all_project.sh
# ==============================================================================

make BIN_NAME="test_mysql" rebuild


猜你喜欢

转载自blog.csdn.net/LostSpeed/article/details/83789190