Linux QT数据库之登录注册

 

视频链接:https://www.bilibili.com/video/av11673511/

 

main.cpp

#include <QSqlDatabase>
#include <QSqlQuery>
 QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("dbname");
    db.open();
    QString s=QString("create table user(name varchar(10) primary key,passwd varchar(10))");
    QSqlQuery query;
    query.exec(s);
 Widget w;
    myDialog d;
    if(d.exec()==myDialog::Accepted)
    {
        w.show();
        return a.exec();
    }

 

 

mydialog.cpp

#include <QSqlQuery>
#include <ui_mydialog.h>
#include "QMessageBox"
void myDialog::on_pushButton_clicked()
{
    QString uname=ui->lineEdit->text();
    QString upasswd=ui->lineEdit_2->text();
    QString s=QString("select * from user where name=='%1' and passwd=='%2' ").arg(uname).arg(upasswd);
    QSqlQuery query;
    query.exec(s);
    if(query.first())
    {
        accept();
    }
    else
    {
        QMessageBox::warning(this,tr("Waring"),tr("pleaer register"),QMessageBox::Yes);
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
        ui->lineEdit->setFocus();
    }
}

void myDialog::on_pushButton_2_clicked()
{
    QString uname=ui->lineEdit->text();
    QString upasswd=ui->lineEdit_2->text();
    QString s=QString("insert into user values ('%1' ,'%2') ").arg(uname).arg(upasswd);
    QSqlQuery query;
    bool d=query.exec(s);
    QMessageBox box;
    if(d)
    {
        box.setText("register success");
        box.exec();
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
        ui->lineEdit->setFocus();
    }
}

 

posted @ 2017-06-26 10:10  蜀云泉  阅读(1479)  评论(0编辑  收藏  举报