#include "database.h"
#include <QtGui/QMessageBox>
ServerDatabase::ServerDatabase()
{
bool create = !QFile::exists("haoDatabase.db");
db = QSqlDatabase::addDatabase("QSQLITE"); //添加数据库驱动
db.setDatabaseName("haoDatabase.db"); //数据库连接命名
if (!db.open())
{
QMessageBox::warning(NULL, tr("Error"),
tr("Can not open the sqlite database"));
exit(2);
}
if(create)
createTable();
}
ServerDatabase::~ServerDatabase()
{
}
void ServerDatabase::createTable()
{
QSqlQuery query;
//用户表(帐号,密码,呢称,头像编号,状态,手机号码,电话号码,
//生日,城市,自我描述)
query.exec("create table users(account varchar(21) primary key, "
"password varchar(21) not null, "
"nickname nvarchar(41) not null, "
"avatarnumber integer not null, "
"status integer not null, "
"mobilenumber varchar(21) not null, "
"phonenumber varchar(21) not null, "
" birthday varchar(20) not null, "
"city nvarchar(41) not null, "
"about nvarchar(201) not null)");
errorSQLOrder(query, "createTable1");
//好友表(好友A,好友B,关系,备注)
query.exec("create table friends(one varchar(21) not null, "
"two varchar(21) not null, "
"friendKind integer not null, "
"remark varchar(101) not null)");
errorSQLOrder(query, "createTable2");
//临时信息表(信息类型,发送者,接收者,信息)
query.exec("create table save(kind integer not null, "
"sender varchar(21) not null, "
"receiver varchar(21) not null, "
"message varchar(5001))");
errorSQLOrder(query, "createTable3");
}
void ServerDatabase::errorSQLOrder(QSqlQuery query, QString mark)
{
//如果SQL语句错误,就弹出警告信息
if(!query.isActive())
{
QString str = query.lastError().text() + "\n" + mark;
QMessageBox::warning(NULL, "ERROR", str);
}
}
void ServerDatabase::initiDatabase()
{
QSqlQuery query;
query.exec("update users set status=0");
errorSQLOrder(query, "initiDatabase1");
}
void ServerDatabase::loginSuccess(QSqlQuery &query, const LoginInformation &logInf,
QVector<FriendInformation> &friendsVec)
{
friendsVec.clear();
//先获得自己的信息
FriendInformation fri;
fri.account = logInf.account;
fri.status = logInf.status;
fri.name = query.value(NAME).toString();
fri.avatarNumber = query.value(AVATAR_NUMBER).toInt();
fri.about = query.value(ABOUT).toString();
fri.friendKind = MYSELF;
friendsVec.push_back(fri);
//更新自己的状态
query.prepare("update users set status=:sta where account=:acc");
query.bindValue(":acc", logInf.account);
query.bindValue(":sta", QString::number(logInf.status));
query.exec();
errorSQLOrder(query, "loginSuccess1");
//通过外连接获得好友的信息
query.prepare("select * from users left join friends on "
"friends.two=users.account where friends.one=:acc");
query.bindValue(":acc", logInf.account);
query.exec();
errorSQLOrder(query, "loginSuccess2");
while(query.next())
{
fri.friendKind = query.value(12).toInt();
if(0 == fri.friendKind)
continue;
fri.account = query.value(ACCOUNT).toString();
fri.name = query.value(NAME).toString();
fri.avatarNumber = query.value(AVATAR_NUMBER).toInt();
fri.status = query.value(STATUS).toInt();
fri.about = query.value(ABOUT).toString();
fri.remark = query.value(13).toString();
friendsVec.push_back(fri);
}
}
void ServerDatabase::addMessageRequest(const Message &mes)
{
QSqlQuery query;
//将信息存入数据库
query.prepare("insert into save values(:kind, :sender, :receiver, :text)");
query.bindValue(":kind", QString::number(mes.kind));
query.bindValue(":sender", mes.sender);
query.bindValue(":receiver", mes.receiver);
query.bindValue(":text", mes.text);
query.exec();
errorSQLOrder(query, "addMessageRequest1");
}
qint32 ServerDatabase::loginRequest(const LoginInformation &logInf,
QVector<FriendInformation> &friendsVec)
{
QSqlQuery query;
int have = 0;
//判断有没有这个用户
query.prepare("select * from users where account=:account");
query.bindValue(":one", logInf.account);
query.exec();
errorSQLOrder(query, "loginRequest1");
if(query.next())
have++;
if(0 == have)
return LOGIN_NO_ACCOUNT;
//判断密码是否正确
else if(query.value(PASSWORD).toString() != logInf.password)
return LOGIN_WRONG_PWD;
//判断用户是否已经登录了
else if(query.value(STATUS) != OFFLINE)
return HAVE_LOGINED;
else
{
loginSuccess(query, logInf, friendsVec);
return LOGIN_SUCCESS;
}
}
qint32 ServerDatabase::registerRequest(const UserInformation &userInf)
{
qint32 replyKind;
QSqlQuery query;
query.prepare("select count(account) from users where account=:account");
query.bindValue(":account", userInf.account);
query.exec();
errorSQLOrder(query, "registerRequest1");
query.next();
//判断该用户名是否已经注册
if(query.value(0).toInt() != 0)
{
replyKind = REGISTER_EXIST;
return replyKind;
}
//添加用户
query.prepare("insert into users values(:acc, :pwd, :name, :avatar, :status,"
":mobile, :phone, :birthday, :city, :about)");
query.bindValue(":acc", userInf.account);
query.bindValue(":pwd", userInf.password);
query.bindValue(":name", userInf.nickName);
query.bindValue(":avatar", QString::number(userInf.avatarNumber));
query.bindValue(":status", QString::number(userInf.status));
query.bindValue(":mobile", userInf.mobileNumber);
query.bindValue(":phone", userInf.phoneNumber);
query.bindValue(":birthday", userInf.birthday);
query.bindValue(":city", userInf.city);
query.bindValue(":about", userInf.aboutMyself);
query.exec();
errorSQLOrder(query, "registerRequest2");
replyKind = REGISTER_SUCCESS;
return replyKind;
}
qint32 ServerDatabase::quitRequest(const QString &acc)
{
QSqlQuery query;
//更新用户状态
query.prepare("update users set status=0 where account=:acc");
query.bindValue(":acc", acc);
query.exec();
errorSQLOrder(query, "quitRequest1");
changeStatusRequest(acc, OFFLINE);
return 0;
}
qint32 ServerDatabase::messageRequest(const Message &mes)
{
addMessageRequest(mes);
return 0;
}
qint32 ServerDatabase::addFriendRequest(const Message &mes)
{
QSqlQuery query;
if(REQUEST_FRIEND == mes.kind)
return requestFriend(mes);
if(AGREE_FRIEND == mes.kind)
{
agreeFriend(mes);
return GET_FRIEND_SUCCESS;
}
else if(DISAGREE_FRIEND == mes.kind)
{
disagreeFriend(mes);
return DISAGREE_FRIEND;
}
return 0;
}
qint32 ServerDatabase::checkRequest(const QString &acc,
QVector<Message> &messageVec)
{
QSqlQuery query;
Message mes;
messageVec.clear();
//判断是否有发送给acc的信息
query.prepare("select * from save where receiver=:rec");
query.bindValue(":rec", acc);
query.exec();
errorSQLOrder(query, "checkRequest1");
while(query.next())
{
mes.kind = query.value(KIND).toInt();
mes.sender = query.value(SENDER).toString();
mes.receiver = query.value(RECEIVER).toString();
mes.text = query.value(TEXT).toString();
messageVec.push_back(mes);
}
if(messageVec.isEmpty())
return NO_MESSAGE;
//将发送出去的信息删除
query.prepare("delete from save where receiver=:rec");
query.bindValue(":rec", acc);
query.exec();
errorSQLOrder(query, "checkRequest2");
return HAVE_MESSAGE;
}
qint32 ServerDatabase::getFriendInfRequest(const QString &acc, FriendInformation &fri)
{
QSqlQuery query;
query.prepare("select * from users where account=:acc");
query.bindValue(":acc", acc);
query.exec();
errorSQLOrder(query, "getFriendRequest1");
while(query.next())
{
fri.account = query.value(ACCOUNT).toString();
fri.name = query.value(NAME).toString();
fri.avatarNumber = query.value(AVATAR_NUMBER).toInt();
fri.status = query.value(STATUS).to
没有合适的资源?快使用搜索试试~ 我知道了~
基于Qt的局域网即时通信系统(聊天软件)
5星 · 超过95%的资源 需积分: 47 797 下载量 57 浏览量
2014-08-03
22:51:12
上传
评论 40
收藏 797KB GZ 举报
温馨提示
以前网上找到的一份源代码,但是忘记下载链接了。。我自己完美运行,所以上传分享给大家。供大家参考,互相讨论学习。
资源推荐
资源详情
资源评论
收起资源包目录
基于Qt的局域网即时通信系统(聊天软件) (201个子文件)
client.pro.user.1.3 0B
server.pro.user.1.3 0B
110.bmp 6KB
111.bmp 6KB
108.bmp 6KB
107.bmp 6KB
106.bmp 6KB
112.bmp 6KB
109.bmp 6KB
44.bmp 5KB
29.bmp 5KB
43.bmp 5KB
68.bmp 5KB
100.bmp 5KB
66.bmp 5KB
90.bmp 5KB
113.bmp 5KB
58.bmp 5KB
3.bmp 5KB
28.bmp 5KB
56.bmp 5KB
93.bmp 5KB
98.bmp 5KB
14.bmp 5KB
36.bmp 5KB
60.bmp 5KB
71.bmp 5KB
13.bmp 5KB
19.bmp 5KB
114.bmp 5KB
34.bmp 5KB
48.bmp 5KB
59.bmp 5KB
37.bmp 5KB
84.bmp 5KB
20.bmp 5KB
1.bmp 5KB
83.bmp 5KB
47.bmp 5KB
75.bmp 5KB
89.bmp 5KB
16.bmp 5KB
88.bmp 5KB
4.bmp 5KB
54.bmp 5KB
8.bmp 5KB
51.bmp 5KB
70.bmp 5KB
11.bmp 5KB
33.bmp 5KB
78.bmp 5KB
115.bmp 5KB
30.bmp 5KB
72.bmp 5KB
21.bmp 5KB
85.bmp 5KB
94.bmp 5KB
55.bmp 5KB
91.bmp 5KB
80.bmp 5KB
24.bmp 5KB
86.bmp 5KB
74.bmp 5KB
130.bmp 5KB
129.bmp 5KB
79.bmp 5KB
61.bmp 5KB
116.bmp 5KB
132.bmp 5KB
2.bmp 5KB
53.bmp 5KB
49.bmp 5KB
52.bmp 5KB
18.bmp 5KB
97.bmp 5KB
73.bmp 5KB
65.bmp 5KB
31.bmp 5KB
5.bmp 5KB
32.bmp 5KB
39.bmp 5KB
46.bmp 5KB
57.bmp 5KB
22.bmp 5KB
38.bmp 5KB
63.bmp 5KB
62.bmp 5KB
42.bmp 5KB
26.bmp 5KB
69.bmp 5KB
17.bmp 5KB
64.bmp 5KB
9.bmp 5KB
50.bmp 5KB
95.bmp 5KB
99.bmp 5KB
134.bmp 5KB
10.bmp 5KB
82.bmp 5KB
41.bmp 5KB
共 201 条
- 1
- 2
- 3
云敛容
- 粉丝: 128
- 资源: 57
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页