#include <iostream>
#include <iomanip>
#include <winsock2.h>
#include "include\mysql.h"
//#pragma comment(lib, "libmysql.lib")
using namespace std;
int fetchsqldb(char *mysql_host);
int custom_sql(char *mysql_host, const char *sql_cmd);
int sys_status(char *mysql_host, const char *sql_cmd);
int status_value(char *mysql_host, const char *sql_cmd);
char* chret(void)
{
char *ch = "Mygod!";
return ch;
}
//---定义mysql监控的类接口
class MysqlMonitor
{
public:
int GetVerNo(void);
protected:
int version;
};
int MysqlMonitor::GetVerNo(void)
{
return version;
}
//---main函数开始-----
int main()
{
char input_buf[256] = {0};
char host[256] = {0};
char cmdbuf[1024] = {0};
cout<<"return chars:"<<chret()<<endl;
printf("chars: %s\n", chret());
while(cout<<"input a num, q for quit:"<<endl, gets(input_buf))
{
switch(input_buf[0])
{
case 'q': return 0;
case '1': //输入ip地址,使用固定的sql语句检索结果
cout<<"input host ip:"<<endl;
while(gets(host))
{
fetchsqldb(host);
break;
}
break;
case '2':
//fetchsqldb("192.168.111.101");
//fetchsqldb("192.168.249.132");
fetchsqldb("192.168.1.100");
break;
case '3':// 输入一个sql命令行来检索数据
cout<<"input sql_cmd:"<<endl;
while(gets(cmdbuf))
{
custom_sql("192.168.1.100", cmdbuf);
break;
}
break;
case '4':// 显示 show status的结果
cout<<"show status:"<<endl;
sys_status("192.168.1.100", "show status");
break;
//--设计为类的一个方法 show global status
case '5':
cout<<"input global variable:"<<endl;
while(gets(host))//检索匹配结果
{
sprintf(cmdbuf, "show global status where variable_name like '%s'", host);
cout<<"host: 192.168.1.100 "<<"cmdbuf: "<<cmdbuf<<endl;
cout<<host<<" = "<<status_value("192.168.1.100", cmdbuf)<<endl;
break;
}
break;
//--设计为类的一个方法——show status
case '6':
cout<<"input variable:"<<endl;
while(gets(host))
{
sprintf(cmdbuf, "show status where variable_name like '%s'", host);
cout<<"host: 192.168.1.100 "<<"cmdbuf: "<<cmdbuf<<endl;
cout<<host<<" = "<<status_value("192.168.1.100", cmdbuf)<<endl;
break;
}
break;
//——设计一个 show variables like 'key_buffer_size'; 方法
//——设计一个 show processlist 方法
//——设计一个 show table status 方法
//——设计一个open & close mysql连接的方法
//——设计一个对以上所有类型方法的,另一个输出结果到buf的方法。[优先级低]
default:
cout<<"input invalid!"<<endl;
}
}
return 0;
}
int fetchsqldb(char *mysql_host)
{
int t = 0, i = 0;
char query_buf[2048];
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL mysql;
cout<<"fechsqldb starting..."<<endl;
mysql_init(&mysql);
if(mysql_real_connect(&mysql, mysql_host, "root", "aaa000", "mydbtest", 3306, NULL, 0))
{
cout<<"connect sql server ok!"<<endl;
}
else
{
mysql_errno(&mysql);
const char *s = mysql_error(&mysql);
cout<<s<<endl;
return 1;
}
strcpy(query_buf, "select * from mytable1");
t = mysql_query(&mysql, query_buf);
if(t)
{
cout<<"Error execute query:"<<mysql_error(&mysql)<<endl;
return 1;
}
else
{
cout<<"Query success!"<<endl;
}
res = mysql_use_result(&mysql);
for(i = 0; ; i++)//mysql_field_count(&mysql),i<mysql_num_rows(res) 这个结果不对
{
row = mysql_fetch_row(res);
if(row <= 0)
break;
for(t = 0; t < mysql_num_fields(res); t++)
cout<<setw(16)<<row[t]<<" ";
cout<<""<<endl;
}
cout<<"[rows no] "<<i<<" fetch end!"<<endl;
mysql_close(&mysql);
return 0;
}
int custom_sql(char *mysql_host, const char *sql_cmd)
{
int t = 0, i = 0;
char query_buf[2048];
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL mysql;
cout<<"fechsqldb starting..."<<endl;
mysql_init(&mysql);
if(mysql_real_connect(&mysql, mysql_host, "root", "aaa000", "mydbtest", 3306, NULL, 0))
{
cout<<"connect sql server ok!"<<endl;
}
else
{
mysql_errno(&mysql);
const char *s = mysql_error(&mysql);
cout<<s<<endl;
return 1;
}
strcpy(query_buf, sql_cmd);
t = mysql_query(&mysql, query_buf);
if(t)
{
cout<<"Error execute query:"<<mysql_error(&mysql)<<endl;
return 1;
}
else
{
cout<<"Query success! sql_cmd is:"<<endl<<sql_cmd<<endl;
}
res = mysql_use_result(&mysql);
for(i = 0; ; i++)//mysql_field_count(&mysql),i<mysql_num_rows(res) 这个结果不对
{
row = mysql_fetch_row(res);
if(row <= 0)
break;
for(t = 0; t < mysql_num_fields(res); t++)
cout<<setw(16)<<row[t]<<" ";
cout<<""<<endl;
}
cout<<"[rows no] "<<i<<" fetch end!"<<endl;
mysql_close(&mysql);
return 0;
}
int sys_status(char *mysql_host, const char *sql_cmd)
{
int t = 0, i = 0;
char query_buf[2048];
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL mysql;
cout<<"fechsqldb starting..."<<endl;
mysql_init(&mysql);
if(mysql_real_connect(&mysql, mysql_host, "root", "aaa000", "mydbtest", 3306, NULL, 0))
{
cout<<"connect sql server ok!"<<endl;
}
else
{
mysql_errno(&mysql);
const char *s = mysql_error(&mysql);
cout<<s<<endl;
return 1;
}
strcpy(query_buf, sql_cmd);
t = mysql_query(&mysql, query_buf);
if(t)
{
cout<<"Error execute query:"<<mysql_error(&mysql)<<endl;
return 1;
}
else
{
cout<<"Query success! sql_cmd is:"<<endl<<sql_cmd<<endl;
}
res = mysql_use_result(&mysql);
for(i = 0; ; i++)//mysql_field_count(&mysql),i<mysql_num_rows(res) 这个结果不对
{
row = mysql_fetch_row(res);
if(row <= 0)
break;
for(t = 0; t < mysql_num_fields(res); t++)
cout<<setw(16)<<row[t]<<" ";
cout<<""<<endl;
}
cout<<"[rows no] "<<i<<" fetch end!"<<endl;
mysql_close(&mysql);
return 0;
}
int status_value(char *mysql_host, const char *sql_cmd)
{
int t = 0, i = 0;
char query_buf[2048];
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL mysql;
cout<<"fechsqldb starting..."<<endl;
mysql_init(&mysql);
if(mysql_real_connect(&mysql, mysql_host, "root", "aaa000", "mydbtest", 3306, NULL, 0))
{
cout<<"connect sql server ok!"<<endl;
}
else
{
mysql_errno(&mysql);
const char *s = mysql_error(&mysql);
cout<<s<<endl;
return -1;
}
strcpy(query_buf, sql_cmd);
t = mysql_query(&mysql, query_buf);
if(t)
{
cout<<"Error execute query:"<<mysql_error(&mysql)<<endl;
return -1;
}
else
{
cout<<"Query success! sql_cmd is:"<<endl<<sql_cmd<<endl;
}
res = mysql_use_result(&mysql);
for(i = 0; ; i++)//mysql_field_count(&mysql),i<mysql_num_rows(res) 这个结果不对
{
row = mysql_fetch_row(res);
if(row <= 0)
break;
for(t = 0; t < mysql_num_fields(res); t++)
{
if(1==t)
return atoi(row[t]);
}
cout<<""<<endl;
}
cout<<"[rows no] "<<i<<" fetch end!"<<endl;
mysql_close(&mysql);
return -1;
}