修改OpenCart数据库软件

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值