From bd6a768f4bc8069bc07cb8e34c42b03ee30bf46b Mon Sep 17 00:00:00 2001
From: Qiu Chuntao <kidtad.qiu@topibd.com>
Date: Tue, 22 Sep 2020 08:59:14 +0000
Subject: [PATCH] Add new file

---
 Mainwind.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
 create mode 100644 Mainwind.cpp

diff --git a/Mainwind.cpp b/Mainwind.cpp
new file mode 100644
index 0000000..342bfe9
--- /dev/null
+++ b/Mainwind.cpp
@@ -0,0 +1,96 @@
+#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();
+    }
+}
-- 
2.21.0