闲来无事,写了一个基于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下才有数据的哈。

posted on 2016-01-27 17:46  泥粑  阅读(2844)  评论(0编辑  收藏  举报