连接迭代数据库

连接迭代数据库

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

  1. #include <windows.h>  
  2. #include <C:/mysql-5.5.17-win32/include/mysql.h>  
  3. #include <fstream>  
  4. #include<io.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7. #include <string.h>  
  8. #include <iostream>  
  9. #include <map>  
  10. #include <sstream>  
  11. using namespace std;  
  12.   
  13. /** 
  14.   *从数据库中读出数据放到map中 
  15. */  
  16. typedef map<string , string> M;  
  17. M dataResult;  
  18. M::key_compare keycomp;//比较  
  19. M::iterator itr;  
  20.   
  21.   
  22. /** 
  23. *查询数据库 
  24. */  
  25. int dataDeal(){  
  26.     int t;  
  27.     char *query;  
  28.     MYSQL *conn_ptr;  
  29.     MYSQL_RES *res;  
  30.     MYSQL_ROW row;  
  31.     //puts("Hello World!!!");  
  32.     conn_ptr = mysql_init(NULL);  
  33.     mysql_real_connect(conn_ptr,"localhost","root","root","test",0,NULL,0);  
  34.     query = "select * from filerecord;";  
  35.     t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));  
  36.     printf("t:%d\n",t);  
  37.     res = mysql_store_result(conn_ptr);  
  38.     while((row = mysql_fetch_row(res))){  
  39.         cout<<row[1]<<"  "<<row[2]<<endl;  
  40.         dataResult.insert(M::value_type(row[1],row[2]));//从数据库中获取的数据,放到map中  
  41.     }  
  42.     mysql_free_result(res);  
  43.     mysql_close(conn_ptr);  
  44.   
  45.   
  46. //    map<string,string>::iterator it = dataResult.begin();  
  47. //    while(it != dataResult.end()){  
  48. //            cout<<(*it).first<<"  -  "<<(*it).second<<endl;  
  49. //            it++;  
  50. //    }  
  51.     return 1;  
  52. }  
  53. /** 
  54.   *数据库插入 
  55.   */  
  56. int dataInsert(string start_path,string write_time){  
  57.     MYSQL *conn_ptr;  
  58.     int t;  
  59.     conn_ptr = mysql_init(NULL);  
  60.     mysql_real_connect(conn_ptr,"localhost","root","root","test",0,NULL,0);  
  61.     string tempstr = "insert into filerecord(filename,write_time) values('" +start_path +"','"+write_time+"')";  
  62.     const char *query = tempstr.c_str();//string类型转换成const char* 类型  
  63.     cout<<"query:"<<query<<endl;  
  64.     t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));  
  65.     mysql_close(conn_ptr);  
  66.     return 1;  
  67. }  
  68. /** 
  69.   *数据库更新 
  70.   */  
  71. int dataUpdate(string start_path,string write_time){  
  72.     MYSQL *conn_ptr;  
  73.     int t;  
  74.     conn_ptr = mysql_init(NULL);  
  75.     mysql_real_connect(conn_ptr,"localhost","root","root","test",0,NULL,0);  
  76.     string tempstr = "update filerecord set write_time= '"+ write_time+"' where filename='" +start_path +"'";  
  77.     const char *query = tempstr.c_str();  
  78.     t = mysql_real_query(conn_ptr,query,(unsigned int)strlen(query));  
  79.     mysql_close(conn_ptr);  
  80.     return 1;  
  81. }  
  82. /** 
  83.   *复制一份文件 
  84.   */  
  85. int copyFile(string fileSource, string fileDest)  
  86. {  
  87.     cout << "copyFile!" << endl;  
  88.     const char * sre = fileSource.c_str();  
  89.     const char * des = fileDest.c_str();  
  90.     ifstream inClientFile(sre,ios::in);//读文件流,sre为要打开的文件  
  91.     ofstream out(des,ios::out);//写文件流,des为要写入的文件  
  92.     if(!inClientFile){  
  93.         cerr<<"File is not be opened"<<endl;  
  94.     }  
  95.     char buf[1024];  
  96.     while(inClientFile.getline(buf,1024)){//读取文件内容  
  97.         out<<buf;//一行数据写到目标文件中  
  98.         out<<"\r";//换行  
  99.     }  
  100.     return 0;  
  101. }  
  102.   
  103.   
  104. /** 
  105.   *递归轮询所有文件,对文件进行操作 
  106.   */  
  107. string respath = "*.*";  
  108. string path = "D:/workspace11/koubei-coupon-web/src/main/webapp/";  
  109. string destpath="D:/dest/src/main/webapp/";  
  110. void displayFile(_finddata_t file,string path){  
  111.   
  112.     string st =path+respath;  
  113.     //cout<<path;  
  114.     const char* p = st.c_str();  
  115.     long lf;  
  116.     if((lf = _findfirst(p, &file))==-1l)//_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)  
  117.         cout<<"文件没有找到!\n";  
  118.     else  
  119.     {  
  120.         //cout<<"\n文件列表:\n";  
  121.         while( _findnext( lf, &file ) == 0 )//int __cdecl _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1  
  122.         {  
  123.             if(file.attrib == _A_SUBDIR){  
  124.                 if(strcmp(file.name,"..")==0 || strcmp(file.name,".svn")==0){  
  125.                 }else{  
  126.                     //cout<<"  子目录  ";  
  127.                     //递归轮询目录下的文件  
  128.                     string temp = path;  
  129.                     temp = temp + file.name + "/";  
  130.                     displayFile(file,temp);  
  131.                     temp = path;  
  132.                 }  
  133.             }  
  134.             string str(file.name);  
  135.             int te = str.find(".jsp");  
  136.             if(te != -1){  
  137.                 //cout<<file.name<<" ";  
  138.                 //cout<<file.time_write<<endl;  
  139.                 stringstream ss;  
  140.                 string ftw ;  
  141.                 ss<<file.time_write;  
  142.                 ss>>ftw;  
  143.                 string start_path = path + file.name;  
  144.                 string end_path = destpath + file.name;  
  145.                 cout<<"目标地址:"<<end_path<<endl;  
  146.   
  147.                 //keycomp = dataResult.key_comp();//map的key值比较  
  148.                 //M::value_compare valuecomp = dataResult.value_comp();//map的value值比较器  
  149. //                cout<<"start_path:"<<endl<<start_path<<endl;  
  150.                 itr = dataResult.find(start_path);  
  151.                 if(itr != dataResult.end()){  
  152.                     //插入数据库操作  
  153.                     cout<<"**********插入数据库操作**********   "<<start_path<<endl;  
  154.                     dataInsert(start_path,ftw);  
  155.                     copyFile(start_path,end_path);  
  156.                     continue;  
  157.                 }  
  158.                 string fir = (*itr).first;  
  159.                 string restime = (*itr).second;  
  160. //                cout<<dataResult.value_comp()((*itr),*itr++)<<fir<<"    "<<restime<<"            fjdkfjdkfdjkf"<<endl;  
  161.   
  162.                 if(restime.compare(ftw)){  
  163.                     //比较时间戳  
  164.                     cout<<"时间:"<<restime<<"  "<<ftw<<endl;  
  165.                     dataUpdate(start_path,ftw);  
  166.                     copyFile(start_path,end_path);  
  167.                 }  
  168.                 //copyFile(path+file.name,destpath+file.name);//实现文件复制,推的方式  
  169.             }  
  170.   
  171.   
  172.         }  
  173.     }  
  174.     _findclose(lf);  
  175. }  
  176. /** 
  177.   *主函数 
  178.   */  
  179. int main()  
  180. {  
  181.     cout << "Hello world!" << endl;  
  182. //    while(true){  
  183.     //第一步读数据库内容  
  184.     dataDeal();  
  185.     //第二步,轮询所有文件  
  186.     _finddata_t file;  
  187.     displayFile(file,path);  
  188. //    }  
  189.     return 0;  
  190. }  

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值