连接迭代数据库

连接迭代数据库

这段程序的主要目的是连接数据库,查询数据库的内容,然后遍历某个文件下的 .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
    评论
要在Qt中连接Oracle数据库,您需要执行以下步骤: 1. 下载和安装Oracle Instant Client 在Oracle官网上下载并安装Oracle Instant Client。选择与您的操作系统和Oracle版本兼容的版本。 2. 安装Qt的OCI驱动程序 在Qt Creator中打开“选项”窗口,并在“构建和运行”部分下选择“Kits”选项卡。然后,选择您的Qt版本和编译器,并单击“编辑”。 在“编辑项目”对话框中,选择“构建环境”选项卡。在“环境变量”下,添加名为“LD_LIBRARY_PATH”的新变量,并将其设置为Oracle Instant Client安装目录的路径。 然后,在“配置库”下,添加OCI驱动程序库,将其命名为“QOCI”,并设置其路径为Oracle Instant Client中的OCI库文件所在的目录。 3. 在Qt中编写代码 在Qt中创建一个新的数据库连接,您需要使用QSqlDatabase类。使用以下代码片段创建一个新的连接: ``` QSqlDatabase db = QSqlDatabase::addDatabase("QOCI"); db.setHostName("localhost"); // 设置主机名 db.setDatabaseName("mydatabase"); // 设置数据库名称 db.setUserName("myusername"); // 设置用户名 db.setPassword("mypassword"); // 设置密码 if (!db.open()) { qDebug() << "Failed to connect to database."; } ``` 在这个例子中,我们使用QOCI驱动程序创建了一个新的数据库连接,并设置了主机名、数据库名称、用户名和密码。然后,我们使用open()方法打开连接。如果连接失败,会输出一条消息。 4. 执行SQL查询 一旦您已经建立了连接,就可以使用QSqlQuery类执行SQL查询。使用以下代码片段创建一个新的查询: ``` QSqlQuery query; query.prepare("SELECT * FROM mytable WHERE id = :id"); query.bindValue(":id", 1234); if (!query.exec()) { qDebug() << "Failed to execute query."; } while (query.next()) { QString name = query.value("name").toString(); int age = query.value("age").toInt(); qDebug() << "Name:" << name << ", Age:" << age; } ``` 在这个例子中,我们使用prepare()方法为查询准备了一个占位符。然后,我们使用bindValue()方法将参数绑定到占位符上。最后,我们使用exec()方法执行查询,并使用next()方法迭代结果集。在每次迭代中,我们使用value()方法获取每个列的值,并输出到控制台窗口。 这里是一个简单的例子来演示如何在Qt中连接Oracle数据库。您可以根据自己的需要修改代码以满足您的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值