Commit bd6a768f authored by Qiu Chuntao's avatar Qiu Chuntao

Add new file

parent 23efe094
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
registwind = new Regist(this);
userwind = new UserWindow(this);
init_database();
connect(this,SIGNAL(sendinfo(QSqlDatabase,QString)),registwind,SLOT(getdata(QSqlDatabase,QString)));
}
MainWindow::~MainWindow()
{
db.close();
delete ui;
}
void MainWindow::init_database()
{
//数据库是否存在
if(QSqlDatabase::contains("mysqlite"))
{
//存在
db = QSqlDatabase::database("mysqlite");
}
else
{
//不存在,则创建
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("user.db");
}
//打开数据库
if(db.open())
{
qDebug()<<"open db ok";
}
else
{
qDebug()<<"open error";
}
QSqlQuery sql_query(db);
QString sql = "create table if not exists "
"user(id integer primary key autoincrement,"
"username text not NULL ,password text not NULL);";
if(!sql_query.exec(sql))
{
qDebug()<<"creat table ok";
return;
}
qDebug()<<"creat ok";
}
//注册
void MainWindow::on_pushButton_2_clicked()
{
emit sendinfo(db,"user");
registwind->show();
this->hide();
}
//登录
void MainWindow::on_pushButton_clicked()
{
//查询数据库
QString username = ui->username->text();
QString password = ui->password->text();
QString sql = QString("select * from user where username = %1 "
"and password = %2;").arg(username,password);
QSqlQuery sql_query(db);
if(!sql_query.exec(sql))
{
qDebug() << "查询失败" << sql_query.lastError();
return;
}
qDebug() << "查询成功";
QString usname,psword;
while(sql_query.next())
{
usname = sql_query.value(1).toString();
psword = sql_query.value(2).toString();
}
qDebug() << usname << psword;
if(usname == username && psword == password)
{
qDebug() << "登录成功";
emit sendata(db,"user");
userwind->show();
this->hide();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment