QT读写Sqlite数据库

QT读写Sqlite数据库,第1张

概述QT读写Sqlite数据库     //.h   /**************************************************************************** ** ** Date    : 2010-05-08 ** Author  : furtherchan ** If you have any questions , please contact

QT读写sqlite数据库

//.h

/****************************************************************************
**
** Date : 2010-05-08
** Author : furtherchan

** If you have any questions,please contact me
**
****************************************************************************/

#ifndef sqlITEDB_H
#define sqlITEDB_H

#include <QObject>
#include <QsqlDatabase>
#include <Qsqlquery>
#include <QsqlqueryModel>
#include <QMessageBox>
#include <QString>
#include <QStringList>

class sqliteDB : public QObject
{

public:
sqliteDB();
QStringList strListUser;
QStringList strListID;
QStringList strListname;

voID connectDB();
voID closeDB();
voID getUserInfo( QString ID );
voID updateUserLogStat( QString ID,QString stat );
int insertNewUser( QString ID,QString password,QString name,QString ip,QString port);
voID getUserAllOnline();
voID updateUserIp(QString ID,QString ip);


private:
QsqlDatabase db;

};

#endif // sqlITEDB_H

//.cpp

/****************************************************************************
**
** Date : 2010-05-08
** Author : furtherchan

** If you have any questions,please contact me
**
****************************************************************************/

#include "sqlitedb.h"

sqliteDB::sqliteDB()
{
}

voID sqliteDB::connectDB()
{
db = QsqlDatabase::addDatabase("QsqlITE");
db.setDatabasename("chat.db");
if ( !db.open())
{
QMessageBox::critical(NulL,"Connect to db...","Connect Failed.");
}
}

voID sqliteDB::closeDB()
{
db.close();
}

voID sqliteDB::getUserInfo(QString ID)
{
this->connectDB();
Qsqlquery query;
strListUser.clear();

if (!(query.exec("SELECT ID,password,name,logstat,ip FROM user")))
{
QMessageBox::critical(NulL,"exec","Exec Failed.");
}

while (query.next())
{
if ( query.value(0).toString() == ID )
{
strListUser.append(query.value(0).toString());
strListUser.append(query.value(1).toString());
strListUser.append(query.value(2).toString());
strListUser.append(query.value(3).toString());
strListUser.append(query.value(4).toString());
}
}
this->closeDB();
}

voID sqliteDB::updateUserLogStat(QString ID,QString stat)
{
this->connectDB();
Qsqlquery query;
strListUser.clear();
if(!(query.prepare("UPDATE user SET logstat = :stat WHERE ID = :ID")))
{
QMessageBox::critical(NulL,"prepare","Prepare Failed.");
}
query.bindValue(":ID",ID);
query.bindValue(":stat",stat);
if(!query.exec())
{
QMessageBox::critical(NulL,"Exec Failed.");
}
this->closeDB();
}

voID sqliteDB::updateUserIp(QString ID,QString ip)
{
this->connectDB();

Qsqlquery query;
strListUser.clear();

if(!(query.prepare("UPDATE user SET ip = :ip WHERE ID = :ID")))
{
QMessageBox::critical(NulL,"Prepare Failed."+ID);
}
query.bindValue(":ID",ID);
query.bindValue(":ip",ip);
if(!query.exec())
{
QMessageBox::critical(NulL,"Exec Failed.");
}

this->closeDB();
}

voID sqliteDB::getUserAllOnline()
{
this->connectDB();
Qsqlquery query;
strListID.clear();
strListname.clear();
if(!(query.prepare("SELECT ID,name FROM user WHERE logstat = :logstat")))
{
QMessageBox::critical(NulL,"Prepare Failed.");
}
query.bindValue(":logstat","1");
if(!query.exec())
{
QMessageBox::critical(NulL,"Exec Failed.");
}
while (query.next())
{
strListID.append(query.value(0).toString());
strListname.append(query.value(1).toString());
}
this->closeDB();

}

int sqliteDB::insertNewUser( QString ID,QString port)
{
this->connectDB();
Qsqlquery query;
if (!(query.exec("SELECT ID FROM user")))
{
QMessageBox::critical(NulL,"Exec Failed.");
return -1;
}
while (query.next())
{
if ( query.value(0).toString() == ID )
{
return 0;
}
}
query.prepare("INSERT INTO user (ID,ip,port,logstat)" "VALUES (:ID,:password,:name,:ip,:port,:logstat)");
query.bindValue(":ID",ID);
query.bindValue(":password",password);
query.bindValue(":name",name);
query.bindValue(":ip",ip);
query.bindValue(":port",port);
query.bindValue(":logstat","0");
query.exec();

this->closeDB();

return 1;}

总结

以上是内存溢出为你收集整理的QT读写Sqlite数据库全部内容,希望文章能够帮你解决QT读写Sqlite数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1177834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存