Commit b69d1320 authored by Qiu Chuntao's avatar Qiu Chuntao

Add new file

parent 5f2b295d
#include "userwindow.h"
#include "ui_userwindow.h"
UserWindow::UserWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::UserWindow)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose,1);
table = new ShowTable(this);
connect(this,SIGNAL(sendata(QSqlDatabase,QString)),table,SLOT(dowork(QSqlDatabase,QString)));
}
UserWindow::~UserWindow()
{
delete ui;
}
void UserWindow::getdata(QSqlDatabase db1,QString tablename1) {
db = db1;
tablename = tablename1;
}
void UserWindow::on_usrtable_clicked()
{
emit sendata(db,"user");
table->show();
}
//根据username查找用户,并将用户放入一张新表单显示到tableview窗口
void UserWindow::on_find_usr_clicked()
{
QSqlDatabase db2;
QSqlQuery sql_query(db2);
QString sql = "create table if not exists "
"temtable(id integer primary key autoincrement,"
"username text not NULL ,password text not NULL);";
if(!sql_query.exec(sql))
{
qDebug()<<"creat table ok";
return;
}
bool ok;
QString text = QInputDialog::getText(this, tr("查询"),
tr("查找用户"), QLineEdit::Normal,
tr("请输入用户名"), &ok);
QString str = QString("SELECT * FROM user WHERE username = '%1'").arg(text);
QSqlQuery query(QSqlDatabase::database("HB"));
if(!query.exec(str))
{
qDebug()<<str;
qDebug()<<"find failed";
}
while(query.next())
{
QString datasql = QString("insert into temtable(username,password) "
"values('%1',%2);").arg(query.value(1).toString()).arg(query.value(2).toString());
qDebug() << datasql;
QSqlQuery sql_query(db2);
if(!sql_query.exec(datasql))
{
qDebug() << "插入失败" << sql_query.lastError();
return;
}
qDebug()<< "插入成功";
emit sendata(db2,"temtable");
table->show();
datasql = QString("TRUNCATE TABLE temtable");
QSqlQuery query(db2);
qDebug()<<datasql;
if(!query.exec(datasql))
{
qDebug() << "删除成功" << query.lastError();
return;
}
qDebug()<< "删除成功";
}
}
void UserWindow::on_delet_usr_clicked()
{
bool ok;
QString text = QInputDialog::getText(this, tr("查询"),
tr("查找用户"), QLineEdit::Normal,
tr("请输入用户名"), &ok);
QString str = QString("DELETE FROM user WHERE username = '%1'").arg(text);
QSqlQuery sql_query(db);
if(!sql_query.exec(str))
{
qDebug() << "删除成功" << sql_query.lastError();
return;
}
qDebug()<< "删除成功";
}
void UserWindow::on_goback_clicked()
{
this->parentWidget()->show();
this->close();
}
void UserWindow::on_updata_usr_clicked()
{
bool ok;
QString text = QInputDialog::getText(this, tr("查询"),
tr("查找用户"), QLineEdit::Normal,
tr("请输入需要修改的用户"), &ok);
int ret = QMessageBox::question(this, tr("问题"),
"修改用户名/密码?(Yes-用户名|No-密码)",
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if(ret == QMessageBox::Yes )
{
QString newname = QInputDialog::getText(this, tr("查询"),
tr("查找用户"), QLineEdit::Normal,
tr("请输入要修改的用户名"), &ok);
QString str = QString("UPDATE user SET username = '%1' WHERE username = '%2'").arg(newname).arg(text);
QSqlQuery sql_query(db);
if(!sql_query.exec(str))
{
qDebug() << "修改成功" << sql_query.lastError();
return;
}
qDebug()<< "修改成功";
}
if(ret == QMessageBox::No)
{
QString newpassword = QInputDialog::getText(this, tr("查询"),
tr("查找用户"), QLineEdit::Normal,
tr("请输入需要修改的密码"), &ok);
QString str = QString("UPDATE user SET password = '%1' WHERE username = '%2'").arg(newpassword).arg(text);
QSqlQuery sql_query(db);
if(!sql_query.exec(str))
{
qDebug() << "修改成功" << sql_query.lastError();
return;
}
qDebug()<< "修改成功";
}
}
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