閑來無事,寫了一個基於sqlite的資料庫管理小軟體。 先上圖 中心思想就是: 創建一個資料庫 然後每一個分組對應一個數據表 然後遍歷該數據表。將名字以treewidgetItem顯示出來。添加刪除實質上是對資料庫在操作。不想寫太多功能,寫多了就成了資料庫的桌面管理版了(嘻嘻開玩笑的題外話)。 下麵
閑來無事,寫了一個基於sqlite的資料庫管理小軟體。
先上圖
中心思想就是:
創建一個資料庫 然後每一個分組對應一個數據表 然後遍歷該數據表。將名字以treewidgetItem顯示出來。添加刪除實質上是對資料庫在操作。不想寫太多功能,寫多了就成了資料庫的桌面管理版了(嘻嘻開玩笑的題外話)。
下麵貼代碼:
Administrater::Administrater(QWidget *parent) : QMainWindow(parent) { QSqlDatabase Administrater=QSqlDatabase::addDatabase("QSQLITE"); Administrater.setDatabaseName("Administrater.db"); Administrater.open(); _query=QSqlQuery(Administrater); _query.prepare("create table goldVip(name varchar(20),sex varchar(10),ID char(20))"); _query.exec(); _query.prepare("create table Vip(name varchar(50),sex varchar(10),ID char(20))"); _query.exec(); _query.prepare("create table Coustomer(name varchar(20),sex varchar(10),ID char(20))"); _query.exec(); _query.prepare("select count(*) from goldVip"); _query.exec(); _query.next(); _a=_query.value(0).toInt(); _query.prepare("select count(*) from Vip"); _query.exec(); _query.next(); _b=_query.value(0).toInt(); _query.prepare("select count(*) from Coustomer"); _query.exec(); _query.next(); _c=_query.value(0).toInt(); this->setGeometry(400,200,600,400); _treeWidget = new QTreeWidget(this); _treeWidget->setColumnCount(1); _treeWidget->setHeaderLabel("聯繫人"); _treeWidget->setStyleSheet("color: blue;" "background-color: yellow;" "selection-color: yellow;" "selection-background-color: blue;"); _Item=new QTreeWidgetItem(_treeWidget); _Item->setText(0,"黃金vip"); // _Item->setCheckState(0, Qt::Checked);//覆選框 _Item->setIcon(0,QIcon("1.ico")); int i=0; _query.prepare("select * from goldVip"); _query.exec(); while(i<_a) { _query.next(); QTreeWidgetItem *Item=new QTreeWidgetItem; Item->setText(0,_query.value(0).toString()); _Item->addChild(Item); ++i; } // connect(_Item,SIGNAL(itemClicked(QTreeWidgetItem *Item,int index)),this, // SLOT(slotItem(QTreeWidgetItem *Item,int index))); _Item2=new QTreeWidgetItem(_treeWidget); _Item2->setText(0,"普通vip"); _Item2->setIcon(0,QIcon("2.ico")); i=0; _query.prepare("select * from Vip"); _query.exec(); while(i<_b) { _query.next(); QTreeWidgetItem *Item=new QTreeWidgetItem; Item->setText(0,_query.value(0).toString()); _Item2->addChild(Item); ++i; } _Item3=new QTreeWidgetItem(_treeWidget); _Item3->setText(0,"普通客戶"); _Item3->setIcon(0,QIcon("3.ico")); i=0; _query.prepare("select * from Coustomer"); _query.exec(); while(i<_c) { _query.next(); QTreeWidgetItem *Item=new QTreeWidgetItem; Item->setText(0,_query.value(0).toString()); _Item3->addChild(Item); ++i; } //Administrater.close(); QMenuBar *menuBar=QMainWindow::menuBar(); this->setMenuBar(menuBar); QAction *Action=new QAction("添加",this); QAction *Action2=new QAction("刪除",this); QAction *Action3=new QAction("查詢",this); QList<QAction *>_list; _list<<Action<<Action2<<Action3; menuBar->addActions(_list); connect(Action,SIGNAL(triggered(bool)),this,SLOT(slotAction())); connect(Action2,SIGNAL(triggered(bool)),this,SLOT(slotAction2())); connect(Action3,SIGNAL(triggered(bool)),this,SLOT(slotAction3())); } Administrater::~Administrater() { } void Administrater::resizeEvent(QResizeEvent *) { _treeWidget->setGeometry(0,0,this->width(),this->height()); } void Administrater::slotAction2() { Delete *w2=new Delete; w2->show(); connect(w2,SIGNAL(delete_sucess()),this,SLOT(update_show())); } void Administrater::slotAction() { Deal *w=new Deal; w->show(); w->setStyleSheet("background-color: rgb(255, 252, 162);"); connect(w,SIGNAL(add_sucess()),SLOT(update_show())); } void Administrater::slotAction3() { Find *w3=new Find; w3->show(); } void Administrater::update_show() { delete _Item; delete _Item2; delete _Item3; _Item=new QTreeWidgetItem(_treeWidget); _Item->setText(0,"黃金vip"); // _Item->setCheckState(0, Qt::Checked);//覆選框 _Item->setIcon(0,QIcon("1.ico")); _Item2=new QTreeWidgetItem(_treeWidget); _Item2->setText(0,"普通vip"); _Item2->setIcon(0,QIcon("2.ico")); _Item3=new QTreeWidgetItem(_treeWidget); _Item3->setText(0,"普通客戶"); _Item3->setIcon(0,QIcon("3.ico")); _query.prepare("select count(*) from goldVip"); _query.exec(); _query.next(); _a=_query.value(0).toInt(); _query.prepare("select count(*) from Vip"); _query.exec(); _query.next(); _b=_query.value(0).toInt(); _query.prepare("select count(*) from Coustomer"); _query.exec(); _query.next(); _c=_query.value(0).toInt(); // _Item->removeChild(); int i=0; _query.prepare("select * from goldVip"); _query.exec(); while(i<_a) { _query.next(); QTreeWidgetItem *Item=new QTreeWidgetItem; Item->setText(0,_query.value(0).toString()); _Item->addChild(Item); ++i; } // connect(_Item,SIGNAL(itemClicked(QTreeWidgetItem *Item,int index)),this, // SLOT(slotItem(QTreeWidgetItem *Item,int index))); i=0; _query.prepare("select * from Vip"); _query.exec(); while(i<_b) { _query.next(); QTreeWidgetItem *Item=new QTreeWidgetItem; Item->setText(0,_query.value(0).toString()); _Item2->addChild(Item); ++i; } i=0; _query.prepare("select * from Coustomer"); _query.exec(); while(i<_c) { _query.next(); QTreeWidgetItem *Item=new QTreeWidgetItem; Item->setText(0,_query.value(0).toString()); _Item3->addChild(Item); ++i; } }
#include "Deal.h" #include "ui_Deal.h" #include<QtSql/QSqlDatabase> #include<QSqlQuery> #include<QDebug> #include<QComboBox> #include<QMessageBox> Deal::Deal(QWidget *parent) : QWidget(parent), ui(new Ui::Deal) { ui->setupUi(this); connect(ui->comboBox,SIGNAL(activated(int)), this,SLOT(on_comboBox_activated(int d))); this->setStyleSheet("QLineEdit{background-color: red}"); ui->lineEdit->setStyleSheet("background-color: rgb(188, 255, 207);"); ui->lineEdit_2->setStyleSheet("background-color: rgb(188, 255, 207);"); ui->lineEdit_3->setStyleSheet("background-color: rgb(188, 255, 207);"); } Deal::~Deal() { delete ui; } void Deal::on_pushButton_clicked() { QSqlQuery query; QString str=ui->lineEdit->text(); QString str2=ui->lineEdit_2->text(); QString str3=ui->lineEdit_3->text(); if(_Kind.isEmpty()) { QMessageBox::information(this,"溫馨提示","你還沒有選者客戶分類",QMessageBox::Ok); return; } QString str4=QString("insert into ")+_Kind+QString(" values(")+ QString("\'")+str+QString("\'")+QString(",")+ QString("\'")+str2+QString("\'")+QString(",")+ QString("\'")+str3+QString("\'")+ QString(")"); query.prepare(str4); if(query.exec()) { QMessageBox *messageBox=new QMessageBox(QMessageBox::Information, "溫馨提示","添加成功",QMessageBox::Ok); messageBox->show(); _timer=new QTimer(this); _timer->start(1200); connect(_timer,SIGNAL(timeout()),messageBox,SLOT(close())); emit add_sucess(); } else { qDebug()<<"falied"; } } void Deal::on_comboBox_activated(int s) { if(ui->lineEdit->text().isEmpty()|ui->lineEdit_2->text().isEmpty()| ui->lineEdit_3->text().isEmpty()) { QMessageBox::information(this,"溫馨提示:","你輸入的資料不完整",QMessageBox::Ok); } if(s==0) { _Kind="goldVip"; } else if(s==1) { _Kind="Vip"; } else if(s==2) { _Kind="Coustomer"; } }
#include "Delete.h" #include "ui_Delete.h" #include<QSqlQuery> #include<QDebug> #include<QMessageBox> #include<QTimer> #include<QPalette> Delete::Delete(QWidget *parent) : QWidget(parent), ui(new Ui::Delete) { ui->setupUi(this); } Delete::~Delete() { delete ui; } void Delete::on_pushButton_clicked() { QString str=ui->lineEdit->text(); QString str2="delete from "; QString str3=" where name="; QString str4=str2+_Str+str3+QString("\'")+str+QString("\'"); QSqlQuery query; query.prepare(str4); if(query.exec()) { QMessageBox *messageBox=new QMessageBox(QMessageBox::Information, "溫馨提示","刪除成功",QMessageBox::Ok); messageBox->show(); QTimer *timer=new QTimer(this); timer->start(1200); connect(timer,SIGNAL(timeout()),messageBox,SLOT(close())); emit delete_sucess(); } else { QMessageBox *messageBox=new QMessageBox(QMessageBox::Information, "溫馨提示","該分組下無該用戶", QMessageBox::Ok); } } void Delete::on_comboBox_activated(int index) { if(index==0) { _Str="goldVip"; } else if(index==1) { _Str="Vip"; } else if(index==2) { _Str="Coustomer"; } }
#include "Find.h" #include "ui_Find.h" #include<QSqlQuery> #include<QMessageBox> #include<QPalette> Find::Find(QWidget *parent) : QWidget(parent), ui(new Ui::Find) { ui->setupUi(this); _lists<<"姓名:"<<"性別:"<<"ID:"; QPalette pal=this->palette(); pal.setBrush(QPalette::Background,Qt::green); this->setPalette(pal); } Find::~Find() { delete ui; } void Find::on_pushButton_clicked() { if(_Str.isEmpty()) { QMessageBox::information(this,"警告","你未選擇客戶類型",QMessageBox::Ok); } QString str=ui->lineEdit->text(); QString str2="select * from "+_Str+QString(" where name=") + QString("\'")+str+QString("\'"); QSqlQuery query; query.prepare(str2); if(query.exec()) { query.next(); if(query.value(0).toString().isEmpty()) { QMessageBox::warning(this,"警告","該資料庫查無此人",QMessageBox::Ok); return; } while(!query.value(0).toString().isEmpty()) { for(int i=0;i<3;++i) { ui->textBrowser->append(_lists.at(i)+query.value(i).toString()); } query.next(); } } else { QMessageBox *messageBox=new QMessageBox(QMessageBox::Information, "溫馨提示","該分組下無該用戶", QMessageBox::Ok); } } void Find::on_comboBox_activated(int index) { if(index==0) { _Str="goldVip"; } else if(index==1) { _Str="Vip"; } else if(index==2) { _Str="Coustomer"; } }
順便提一句 sqlite是沒有表頭的 所以用query去提取數據時 先next下才有數據的哈。