#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
// 当前sql语句是否是创建一个表格
bool isCreateTable(string line);
// 获取sql语句中创建表格名字
string getTableName(string line);
// 生成一个删除该表格的sql语句
string getDropLine(string tableName);
/**
* 读取OpenCart.sql,在每一个创建表格的前面先删除该表格.
* 生成一个新的sql文件:OpenCart.sql.out
*/
int main(int argc, char *argv[])
{
string inFileName,outFileName;
cout << "Enter the sql file name: ";
cin >> inFileName;
outFileName = inFileName + ".out";
cout << "Original SQL file:" << inFileName << "\t"
<< "Output SQL file:" << outFileName << endl;
ifstream fin;
ofstream fout(outFileName.c_str());
fin.open(inFileName.c_str());
if(!fin.is_open()){
cout << "The file:" << inFileName << " open faild." << endl;
return -1;
}
string line;
string tableName;
string DropLine;
while(!fin.eof()){
getline(fin,line);
if(isCreateTable(line)){
tableName = getTableName(line);
DropLine = getDropLine(tableName);
fout << DropLine << endl;
fout << line << endl;
} else {
fout << line << endl;
}
}
return 0;
}
bool isCreateTable(string line){
string ctable = "CREATE TABLE IF NOT EXISTS";
int pos = line.find(ctable,0);
if(pos != string::npos){
return true;
}
return false;
}
string getTableName(string line){
string tableName = "";
int pos1 = line.find('`');
int pos2 = line.find('`',pos1+1);
if(pos1 != string::npos || pos2 != string::npos){
tableName = line.substr(pos1+1,pos2-pos1-1);
}
return tableName;
}
string getDropLine(string tableName){
string tName = "TableName";
string dropTable = "DROP TABLE IF EXISTS `TableName`;";
int pos = dropTable.find(tName,0);
dropTable.replace(pos,tName.length(),tableName);
return dropTable;
}
修改OpenCart数据库软件
最新推荐文章于 2021-03-09 23:59:59 发布