std::string XClient::Input()
{
//清空缓冲
//cin.ignore(4096, '\n');
string input = "";
for (;;)
{
char a = getchar();
if (a <= 0 || a == '\n' || a == '\r')
break;
cout << "*" << flush;
input += a;
}
return input;
}
for (;;)
{
cout << "Input: " << flush;
string cmd = Input();
cout << cmd << endl;
//格式 log 1 10 第一页 1页10行
//切割空格 通过空格把内容切成一块一块的
vector<string>cmds;
char* p = strtok((char*)cmd.c_str(), " ");
while (p)
{
cmds.push_back(p);
p = strtok(0, " ");
}
string type = cmd;
if (cmds.size() > 0)
type = cmds[0];
if (type == "log")
{
c_log(cmds);
}
}
C 库函数?char *strtok(char *str, const char *delim)?分解字符串?str?为一组字符串,delim?为分隔符。
//log 1 10 第一页 每页10条
void XClient::c_log(std::vector<std::string> cmds)
{
int pagecount = 10;
int page = 1;
if (cmds.size() > 1)
page = atoi(cmds[1].c_str());
if (cmds.size() > 2)
pagecount = atoi(cmds[2].c_str());
cout << "In log" << endl;
//limit 0 10 从0开始取10条语句
string sql = "select * from t_log limit ";
stringstream ss;
ss <<sql;
//如果是第二页,那么从(2-1)*10=10个开始
ss << (page - 1) * pagecount;
ss << ",";
ss << pagecount;
sql = ss.str();
auto rows = my->GetResult(sql.c_str());
//遍历每一行
for (auto row : rows)
{//遍历每一个字段 遍历每一列
for (auto c : row)
{
//打印出来
cout << c.data << " ";
}
cout << endl;
}
//int total = 0;
// cout << "Total: " << total << endl;
cout << "page= " << page << " pagecount=" << pagecount << endl;
}
select *from t_log limit 25,5