通过输入多行的字符串命令,自动对所输入的mysql关键字进行检索,并输出插入数据库的指令。
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
char* s = new char[1024];
char s1[1024];
int key = 0;
while (cin.getline(s, 1024)) {
if (*s == 0) break;
str += s;
str += '\n';
key++;
}
vector<string> in;
int index_begin = 0, index_end = 0;
for (auto i = str.begin();i != str.end();i++) {
if (*i == ' ' || *i == '\n') {
index_end = index_end;
string s_key(&s1[index_begin], &s1[index_end]);
in.push_back(s_key);
index_begin = index_end;
}
s1[index_end] = *i;
index_end++;
}
for (auto i = 1;i < (in.size() / key);i++) {
cout << "Inset into table_i(" << in[0];
for (int j = 1; j < key; j++) {
cout << ',' << in[j];
}
cout << ") values("<< in[key * i];
for (int j = 1; j < key; j++) {
cout << ',' << in[key * i + j];
}
cout<< ")" << endl;
}
delete[]s;
return 0;
}
且当输入的字符串个数与关键值不相等时,不会产生插入数据库的指令。