QT sqlite3数据库读取、容器操作、文件读写——学习笔记

直接上代码


  1. #include "dbpoi.h"  
  2. #include <QtGui/QApplication>  
  3. #include <QtCore/QCoreApplication>  
  4. #include <QSqlDatabase>  
  5. #include <QSqlQuery>  
  6. #include <QSqlError>  
  7. #include <QSqlDriver>  
  8. #include <qDebug>  
  9. #include <QString>  
  10. #include <QVector>  
  11. #include <QList>  
  12. #include <QFile>  
  13. #include <QSet>  
  14.   
  15.   
  16. int main(int argc, char *argv[])  
  17. {  
  18.     QApplication a(argc, argv);  
  19.     //dbPOI w;  
  20.       
  21.      //QCoreApplication a(argc, argv);  
  22.   //  QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));  
  23.     QVector<int> tempFromNode;  
  24.     QVector<int> tempToNode;  
  25.     QSet <int> tempSort;  
  26.   
  27.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");   
  28.     db.setDatabaseName("NEW_AR_POI.sqlite"); // 数据库名与路径, 此时是放在同目录下  
  29.     // db.setDatabaseName("offset.db"); // 数据库名与路径, 此时是放在同目录下  
  30.     bool ok = db.open(); // 连接数据库, 然后就可以使用了.  
  31.     if(ok){  
  32.         QSqlQuery query;  
  33.             if (query.exec("select * from LinkNode_Table_bark "))   //尝试列出  表的所有记录  
  34.             {  //本次查询成功  
  35.                 int numRows = 0;  //询问数据库驱动,是否驱动含有某种特性   
  36.        
  37.                     if (db.driver()->hasFeature(QSqlDriver::QuerySize))  
  38.                     {  
  39.                         numRows = query.size();  //如果支持结果影响的行数,那么直接记录下来  
  40.                     }  
  41.                     else  
  42.                     {  
  43.                         query.last(); //否则定位到结果最后,qt 文档说,这个方法非常慢  
  44.                         numRows = query.at() + 1;  
  45.                         query.seek(-1);  
  46.                     }  
  47.             }  
  48.                             //尝试列出  表的所有记录  
  49.             while (query.next()) {  
  50.   
  51.                 int num =0;  
  52.                 /*QString FNode_ID = query.value(4).toString(); 
  53.                 QString TNode_ID = query.value(5).toString();*/  
  54.                 int FNode_ID = query.value(3).toInt();  
  55.                 int TNode_ID = query.value(4).toInt();  
  56.   
  57.                 tempFromNode.append(FNode_ID);  
  58.                 tempToNode.append(TNode_ID);                  
  59.   
  60.             }  
  61.         }  
  62.         else{  
  63.           //qDebug() << "cannot open database.";  
  64.             printf( "cannot open database.");  
  65.     }  
  66.           
  67.         QFile file( "file.txt" );  
  68.             if ( file.open(QIODevice::WriteOnly | QIODevice::Text)) {  
  69.                 QTextStream stream( &file );   
  70.                 for ( int index =0; index < tempFromNode.size(); index ++ ){  
  71.                     stream << tempFromNode.at(index);  
  72.                     tempSort.insert(tempFromNode.at(index));  
  73.                         tempSort.insert(tempToNode.at(index));  
  74.                     stream<<"\t"<< tempToNode.at(index) <<endl ;  
  75.                                     }  
  76.                 file.close();  
  77.             }  
  78.           
  79.         //  qSort(tempSort.begin(),tempSort.end());  
  80.             //tempSort.unite(tempSort);  
  81. QFile filetTempSort( "tempSort.txt" );  
  82.             if ( filetTempSort.open(QIODevice::WriteOnly | QIODevice::Text)) {  
  83.                 QTextStream stream( &filetTempSort );   
  84.                   
  85.                          foreach (const int &value, tempSort)  
  86.                          {  
  87.                              stream << value<<endl;    
  88.                              filetTempSort.close();  
  89.                     }  
  90.       
  91.             }  
  92.   
  93.             QFile fileTp( "TP.txt" );  
  94.             if ( fileTp.open(QIODevice::WriteOnly | QIODevice::Text)) {  
  95.                 QTextStream stream( &fileTp);   
  96.                   
  97.                  foreach (const int &value, tempSort)  
  98.                  {  
  99.                      stream << value;  
  100.   
  101.                      for ( int index =0; index < tempFromNode.size(); index ++ ){  
  102.                          if (tempFromNode.at(index) == value)    
  103.                              stream << "\t"<<tempToNode.at(index);  
  104.                      }  
  105.                       
  106.                     for ( int index =0; index < tempFromNode.size(); index ++ ){  
  107.                         if (tempToNode.at(index) == value)    
  108.                              stream << "\t"<<tempFromNode.at(index);  
  109.                      }  
  110.                          stream << endl;  
  111.                     }  
  112.                   
  113.                  fileTp.close();  
  114.             }  
  115.   
  116.         //w.show();  
  117.         //return a.exec();  
  118.         return 0;  
  119.   
  120. }  
  121.       
http://blog.csdn.net/swqqcs/article/details/7677781
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值