//.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"
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;}