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

前端之家收集整理的这篇文章主要介绍了QT sqlite3数据库读取、容器操作、文件读写——学习笔记。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

直接上代码


#include "dbpoi.h"
#include <QtGui/QApplication>
#include <QtCore/QCoreApplication>
#include <QsqlDatabase>
#include <QsqlQuery>
#include <QsqlError>
#include <QsqlDriver>
#include <qDebug>
#include <QString>
#include <QVector>
#include <QList>
#include <QFile>
#include <QSet>


int main(int argc,char *argv[])
{
	QApplication a(argc,argv);
	//dbPOI w;
	
	 //QCoreApplication a(argc,argv);
  //  QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
	QVector<int> tempFromNode;
	QVector<int> tempToNode;
	QSet <int> tempSort;

    QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE"); 
    db.setDatabaseName("NEW_AR_POI.sqlite"); // 数据库名与路径,此时是放在同目录下
	// db.setDatabaseName("offset.db"); // 数据库名与路径,此时是放在同目录下
    bool ok = db.open(); // 连接数据库,然后就可以使用了.
	if(ok){
		QsqlQuery query;
	  		if (query.exec("select * from LinkNode_Table_bark "))   //尝试列出  表的所有记录
			{  //本次查询成功
				int numRows = 0;  //询问数据库驱动,是否驱动含有某种特性 
	 
					if (db.driver()->hasFeature(QsqlDriver::QuerySize))
					{
						numRows = query.size();  //如果支持结果影响的行数,那么直接记录下来
					}
					else
					{
						query.last(); //否则定位到结果最后,qt 文档说,这个方法非常慢
						numRows = query.at() + 1;
						query.seek(-1);
					}
			}
							//尝试列出  表的所有记录
			while (query.next()) {

				int num =0;
				/*QString FNode_ID = query.value(4).toString();
				QString TNode_ID = query.value(5).toString();*/
				int FNode_ID = query.value(3).toInt();
				int TNode_ID = query.value(4).toInt();

				tempFromNode.append(FNode_ID);
				tempToNode.append(TNode_ID);				

			}
		}
		else{
		  //qDebug() << "cannot open database.";
			printf( "cannot open database.");
	}
		
		QFile file( "file.txt" );
			if ( file.open(QIODevice::WriteOnly | QIODevice::Text)) {
				QTextStream stream( &file ); 
				for ( int index =0; index < tempFromNode.size(); index ++ ){
					stream << tempFromNode.at(index);
					tempSort.insert(tempFromNode.at(index));
						tempSort.insert(tempToNode.at(index));
					stream<<"\t"<< tempToNode.at(index) <<endl ;
									}
				file.close();
			}
		
		//	qSort(tempSort.begin(),tempSort.end());
			//tempSort.unite(tempSort);
QFile filetTempSort( "tempSort.txt" );
			if ( filetTempSort.open(QIODevice::WriteOnly | QIODevice::Text)) {
				QTextStream stream( &filetTempSort ); 
				
						 foreach (const int &value,tempSort)
						 {
							 stream << value<<endl;	 
							 filetTempSort.close();
					}
	
			}

			QFile fileTp( "TP.txt" );
			if ( fileTp.open(QIODevice::WriteOnly | QIODevice::Text)) {
				QTextStream stream( &fileTp); 
				
				 foreach (const int &value,tempSort)
				 {
					 stream << value;

					 for ( int index =0; index < tempFromNode.size(); index ++ ){
						 if (tempFromNode.at(index) == value)  
							 stream << "\t"<<tempToNode.at(index);
					 }
					
					for ( int index =0; index < tempFromNode.size(); index ++ ){
						if (tempToNode.at(index) == value)	
							 stream << "\t"<<tempFromNode.at(index);
					 }
						 stream << endl;
					}
				
				 fileTp.close();
			}

		//w.show();
		//return a.exec();
		return 0;

}
	
原文链接:https://www.f2er.com/sqlite/201972.html

猜你在找的Sqlite相关文章