连接数据库+迭代文件

来源:互联网 发布:卖家设置淘宝客 编辑:程序博客网 时间:2024/06/09 17:36

这段程序的主要目的是连接数据库,查询数据库的内容,然后遍历某个文件下的 .jsp文件,如果.jsp文件的最后更新时间与数据库中的更新时间不同,则将数据库的信息更改。

#include <windows.h>#include <C:/mysql-5.5.17-win32/include/mysql.h>#include <fstream>#include<io.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <map>#include <sstream>using namespace std;/**  *从数据库中读出数据放到map中*/typedef map<string , string> M;M dataResult;M::key_compare keycomp;//比较M::iterator itr;/***查询数据库*/int dataDeal(){    int t;    char *query;    MYSQL *conn_ptr;    MYSQL_RES *res;    MYSQL_ROW row;    //puts("Hello World!!!");    conn_ptr = mysql_init(NULL);    mysql_real_connect(conn_ptr,"localhost","root","root","test",0,NULL,0);    query = "select * from filerecord;";    t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));    printf("t:%d\n",t);    res = mysql_store_result(conn_ptr);    while((row = mysql_fetch_row(res))){        cout<<row[1]<<"  "<<row[2]<<endl;        dataResult.insert(M::value_type(row[1],row[2]));//从数据库中获取的数据,放到map中    }    mysql_free_result(res);    mysql_close(conn_ptr);//    map<string,string>::iterator it = dataResult.begin();//    while(it != dataResult.end()){//            cout<<(*it).first<<"  -  "<<(*it).second<<endl;//            it++;//    }    return 1;}/**  *数据库插入  */int dataInsert(string start_path,string write_time){    MYSQL *conn_ptr;    int t;    conn_ptr = mysql_init(NULL);    mysql_real_connect(conn_ptr,"localhost","root","root","test",0,NULL,0);    string tempstr = "insert into filerecord(filename,write_time) values('" +start_path +"','"+write_time+"')";    const char *query = tempstr.c_str();//string类型转换成const char* 类型    cout<<"query:"<<query<<endl;    t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));    mysql_close(conn_ptr);    return 1;}/**  *数据库更新  */int dataUpdate(string start_path,string write_time){    MYSQL *conn_ptr;    int t;    conn_ptr = mysql_init(NULL);    mysql_real_connect(conn_ptr,"localhost","root","root","test",0,NULL,0);    string tempstr = "update filerecord set write_time= '"+ write_time+"' where filename='" +start_path +"'";    const char *query = tempstr.c_str();    t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));    mysql_close(conn_ptr);    return 1;}/**  *复制一份文件  */int copyFile(string fileSource, string fileDest){    cout << "copyFile!" << endl;    const char * sre = fileSource.c_str();    const char * des = fileDest.c_str();    ifstream inClientFile(sre,ios::in);//读文件流,sre为要打开的文件    ofstream out(des,ios::out);//写文件流,des为要写入的文件    if(!inClientFile){        cerr<<"File is not be opened"<<endl;    }    char buf[1024];    while(inClientFile.getline(buf,1024)){//读取文件内容        out<<buf;//一行数据写到目标文件中        out<<"\r";//换行    }    return 0;}/**  *递归轮询所有文件,对文件进行操作  */string respath = "*.*";string path = "D:/workspace11/koubei-coupon-web/src/main/webapp/";string destpath="D:/dest/src/main/webapp/";void displayFile(_finddata_t file,string path){    string st =path+respath;    //cout<<path;    const char* p = st.c_str();    long lf;    if((lf = _findfirst(p, &file))==-1l)//_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)        cout<<"文件没有找到!\n";    else    {        //cout<<"\n文件列表:\n";        while( _findnext( lf, &file ) == 0 )//int __cdecl _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1        {            if(file.attrib == _A_SUBDIR){                if(strcmp(file.name,"..")==0 || strcmp(file.name,".svn")==0){                }else{                    //cout<<"  子目录  ";                    //递归轮询目录下的文件                    string temp = path;                    temp = temp + file.name + "/";                    displayFile(file,temp);                    temp = path;                }            }            string str(file.name);            int te = str.find(".jsp");            if(te != -1){                //cout<<file.name<<" ";                //cout<<file.time_write<<endl;                stringstream ss;                string ftw ;                ss<<file.time_write;                ss>>ftw;                string start_path = path + file.name;                string end_path = destpath + file.name;                cout<<"目标地址:"<<end_path<<endl;                //keycomp = dataResult.key_comp();//map的key值比较                //M::value_compare valuecomp = dataResult.value_comp();//map的value值比较器//                cout<<"start_path:"<<endl<<start_path<<endl;                itr = dataResult.find(start_path);                if(itr != dataResult.end()){                    //插入数据库操作                    cout<<"**********插入数据库操作**********   "<<start_path<<endl;                    dataInsert(start_path,ftw);                    copyFile(start_path,end_path);                    continue;                }                string fir = (*itr).first;                string restime = (*itr).second;//                cout<<dataResult.value_comp()((*itr),*itr++)<<fir<<"    "<<restime<<"            fjdkfjdkfdjkf"<<endl;                if(restime.compare(ftw)){                    //比较时间戳                    cout<<"时间:"<<restime<<"  "<<ftw<<endl;                    dataUpdate(start_path,ftw);                    copyFile(start_path,end_path);                }                //copyFile(path+file.name,destpath+file.name);//实现文件复制,推的方式            }        }    }    _findclose(lf);}/**  *主函数  */int main(){    cout << "Hello world!" << endl;//    while(true){    //第一步读数据库内容    dataDeal();    //第二步,轮询所有文件    _finddata_t file;    displayFile(file,path);//    }    return 0;}

连接数据库时不要忘了mysql.h这个文件的位置: