/////////////////////choicemanager.cpp
#include "choiceManager.h"
#include"ui_choiceManager.h"
#include<QSqlDatabase>
#include<QSqlError>
#include<QtDebug>
#include<QMessageBox>
#include<QSqlQuery>
#include<QInputDialog>
#include<QStringList>
//#include"qsql_sqlite.h"
//#include
choiceManager::choiceManager(QString id)
:QDialog(),dialog(new Ui::manager),userId(id)
{
this->setModal(true);
dialog->setupUi(this);
this->show();
connect(dialog->addGeneral,SIGNAL(clicked()),this,SLOT(handleInsertField()));
connect(dialog->addSpecial,SIGNAL(clicked()),this,SLOT(handleInsertSpecial()));
connect(dialog->addPerson,SIGNAL(clicked()),this,SLOT(handleInsertHandman()));
connect(dialog->deleteGeneral,SIGNAL(clicked()),this,SLOT(handleDeleteField()));
connect(dialog->deleteSpecial,SIGNAL(clicked()),this,SLOT(handleDeleteSpecial()));
connect(dialog->deletePerson,SIGNAL(clicked()),this,SLOT(handleDeleteHandman()));
connect(dialog->listWidget,SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),this,SLOT(updateSpecial()));
connection();
updateField();
updateSpecial();
updateHandman();
dialog->listWidget->setCurrentRow(0);
}
choiceManager::~choiceManager()
{
delete dialog;
}
void choiceManager::connection()
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
QStringList tablesEnsure=db->tables();
if(!tablesEnsure.contains(QString("field")))
{
if(!query->exec("CREATE TABLE field(x INTEGER PRIMARY KEY ASC,id,field,special)"))
{
QMessageBox::information(this,"Fail","Fail to creat table field");
qDebug()<<query->lastError();
}
}
if(!tablesEnsure.contains(QString("handman")))
{
if(!query->exec("CREATE TABLE handman(x INTEGER PRIMARY KEY ASC,id,handman)"))
{
QMessageBox::information(this,"Fail","Fail to creat table handman");
qDebug()<<query->lastError();
}
}
if(!tablesEnsure.contains(QString("rowCount")))
{
if(!query->exec("CREATE TABLE rowCount(x INTEGER PRIMARY KEY ASC,id,rowCount INTEGER)"))
{
QMessageBox::information(this,"Fail","Fail to creat table rowCount");
qDebug()<<query->lastError();
}
}
db->close();
//
}
void choiceManager::updateField()
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("SELECT DISTINCT field FROM field WHERE id=:userId");
query->bindValue(":userId",userId);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to get field");
qDebug()<<query->lastError();
db->close();
return;
}
dialog->listWidget->clear();
while(query->next())
{
dialog->listWidget->addItem(query->value(0).toString());
}
if(dialog->listWidget->item(0))
dialog->listWidget->setCurrentItem(dialog->listWidget->item(0));
db->close();
}
void choiceManager::updateSpecial()
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
QListWidgetItem *temp;
if((temp=dialog->listWidget->currentItem())==NULL)
return;
query->prepare("SELECT DISTINCT special FROM field WHERE field=:selectedField AND id=:userId");
query->bindValue(":selectedField",temp->text());
query->bindValue(":userId",userId);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to get special");
qDebug()<<query->lastError();
return;
}
dialog->listWidget_1->clear();
while(query->next())
{
dialog->listWidget_1->addItem(query->value(0).toString());
}
db->close();
}
void choiceManager::updateHandman()
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("SELECT DISTINCT handman FROM handman WHERE id=:userId");
query->bindValue(":userId",userId);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to get handman");
qDebug()<<query->lastError();
return;
}
dialog->listWidget_2->clear();
while(query->next())
{
dialog->listWidget_2->addItem(query->value(0).toString());
}
db->close();
}
void choiceManager::insertField(QString fieldName)
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
db->setConnectOptions("QSQLITE_BUSY_TIMEOUT=1");
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("INSERT INTO field(id,field) VALUES(:userId,:insertedField) ");
query->bindValue(":userId",userId);
query->bindValue(":insertedField",fieldName);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to insert field");
qDebug()<<query->lastError();
}
db->close();
}
void choiceManager::insertSpecial(QString fieldName,QString specialName)
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
db->setConnectOptions("QSQLITE_BUSY_TIMEOUT=1");
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("INSERT INTO field(id,field,special) VALUES(:userId,:insertedField,:insertedSpecial)");
query->bindValue(":userId",userId);
query->bindValue(":insertedField",fieldName);
query->bindValue(":insertedSpecial",specialName);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to insert special");
qDebug()<<query->lastError();
}
db->close();
}
void choiceManager::insertHandman(QString handmanName)
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
db->setConnectOptions("QSQLITE_BUSY_TIMEOUT=1");
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("INSERT INTO handman(id,handman) VALUES(:id,:insertedHandman)");
query->bindValue(":userId",userId);
query->bindValue(":insertedHandman",handmanName);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to get handman");
qDebug()<<query->lastError();
}
db->close();
}
void choiceManager::deleteField(QString fieldName)
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("DELETE FROM field WHERE id=:userId AND field=:deleteField");
query->bindValue(":userId",userId);
query->bindValue(":deleteField",fieldName);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to delete field");
qDebug()<<query->lastError();
}
db->close();
}
void choiceManager::deleteSpecial(QString fieldName,QString specialName)
{
db=new QSqlDatabase();
*db=QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName( "manager.db" );
if( !db->open() )
{
qDebug() << db->lastError()<<db->drivers();
QMessageBox::information(this,"Fail","Fail to connect database");
}
query=new QSqlQuery(*db);
query->prepare("DELETE FROM field WHERE id=:userId AND field=:deletedField AND special=:deletedSpecial");
query->bindValue(":userId",userId);
query->bindValue("deletedField",fieldName);
query->bindValue("deletedSpecial",specialName);
if(!query->exec())
{
QMessageBox::information(this,"Fail","Fail to delete special");
qDebug()<<query->lastError();
}
db->close();
}
void choiceManager::deleteHandman(QString handmanName)