#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).toInt();
fri.about = query.value(ABOUT).toString();
fri.friendKind = VERIFYING;
fri.remark.clear();
}
return GET_FRIEND_SUCCESS;
}
qint32 ServerDatabase::deleteFriendRequest(const QString &myAcc, con
没有合适的资源?快使用搜索试试~ 我知道了~
我自己写的聊天软件haoIM的源代码
共187个文件
bmp:134个
cpp:20个
h:20个
需积分: 9 23 下载量 75 浏览量
2011-04-17
05:39:51
上传
评论 2
收藏 703KB ZIP 举报
温馨提示
我自己写的聊天软件haoIM的源代码,用C++(QT)实现,欢迎大家下载试用。 有问题或建议,请发送邮件至:aa1080711@163.com
资源推荐
资源详情
资源评论
收起资源包目录
我自己写的聊天软件haoIM的源代码 (187个子文件)
110.bmp 6KB
112.bmp 6KB
106.bmp 6KB
108.bmp 6KB
107.bmp 6KB
111.bmp 6KB
109.bmp 6KB
35.bmp 5KB
38.bmp 5KB
56.bmp 5KB
63.bmp 5KB
8.bmp 5KB
130.bmp 5KB
65.bmp 5KB
81.bmp 5KB
19.bmp 5KB
114.bmp 5KB
11.bmp 5KB
16.bmp 5KB
85.bmp 5KB
82.bmp 5KB
31.bmp 5KB
99.bmp 5KB
44.bmp 5KB
69.bmp 5KB
3.bmp 5KB
75.bmp 5KB
41.bmp 5KB
2.bmp 5KB
100.bmp 5KB
22.bmp 5KB
9.bmp 5KB
30.bmp 5KB
79.bmp 5KB
25.bmp 5KB
97.bmp 5KB
93.bmp 5KB
132.bmp 5KB
60.bmp 5KB
89.bmp 5KB
98.bmp 5KB
57.bmp 5KB
12.bmp 5KB
78.bmp 5KB
15.bmp 5KB
26.bmp 5KB
37.bmp 5KB
86.bmp 5KB
14.bmp 5KB
46.bmp 5KB
10.bmp 5KB
29.bmp 5KB
73.bmp 5KB
52.bmp 5KB
50.bmp 5KB
23.bmp 5KB
6.bmp 5KB
20.bmp 5KB
134.bmp 5KB
27.bmp 5KB
45.bmp 5KB
96.bmp 5KB
4.bmp 5KB
76.bmp 5KB
49.bmp 5KB
115.bmp 5KB
87.bmp 5KB
36.bmp 5KB
7.bmp 5KB
58.bmp 5KB
53.bmp 5KB
55.bmp 5KB
95.bmp 5KB
47.bmp 5KB
21.bmp 5KB
59.bmp 5KB
67.bmp 5KB
54.bmp 5KB
83.bmp 5KB
66.bmp 5KB
72.bmp 5KB
77.bmp 5KB
17.bmp 5KB
28.bmp 5KB
18.bmp 5KB
91.bmp 5KB
5.bmp 5KB
40.bmp 5KB
71.bmp 5KB
51.bmp 5KB
116.bmp 5KB
88.bmp 5KB
1.bmp 5KB
48.bmp 5KB
24.bmp 5KB
68.bmp 5KB
32.bmp 5KB
84.bmp 5KB
61.bmp 5KB
43.bmp 5KB
共 187 条
- 1
- 2
资源评论
AAA20090987
- 粉丝: 580
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功