添加标签管理

修复sql语句错误
This commit is contained in:
WS 2021-08-15 19:09:08 +08:00
parent 86d142cb29
commit 6da7cf4d48
4 changed files with 163 additions and 11 deletions

View File

@ -25,7 +25,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
QSqlQuery q(db_ins); QSqlQuery q(db_ins);
q.prepare("create table if not exists country_information( " q.prepare("create table if not exists country_information( "
"cid integer auto_increment primary key, " "cid integer primary key autoincrement, "
"name text not null, describe text)"); "name text not null, describe text)");
if (!q.exec()) { if (!q.exec()) {
qDebug() << q.lastError(); qDebug() << q.lastError();
@ -33,7 +33,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists publishing_information( " q.prepare("create table if not exists publishing_information( "
"pid integer auto_increment primary key, name text not null, " "pid integer primary key autoincrement, name text not null, "
"country integer not null, describe text, " "country integer not null, describe text, "
"constraint fk_pi foreign key (country) references country_information(cid) " "constraint fk_pi foreign key (country) references country_information(cid) "
"on update cascade on delete cascade)"); "on update cascade on delete cascade)");
@ -43,7 +43,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists authors_information( " q.prepare("create table if not exists authors_information( "
"author_id integer auto_increment primary key, name text not null, " "author_id integer primary key autoincrement, name text not null, "
"country integer not null, describe text, " "country integer not null, describe text, "
"constraint fk_ai foreign key (country) references country_information(cid) " "constraint fk_ai foreign key (country) references country_information(cid) "
"on delete cascade on update cascade)"); "on delete cascade on update cascade)");
@ -53,7 +53,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists books_information(" q.prepare("create table if not exists books_information("
"book_id integer auto_increment primary key,isbn text not null," "book_id integer primary key autoincrement,isbn text not null,"
"name text not null,publishing integer not null,author integer not null," "name text not null,publishing integer not null,author integer not null,"
"author2 integer not null,language text,time text,abstract text,image text," "author2 integer not null,language text,time text,abstract text,image text,"
"constraint fk_bi0 foreign key (publishing) references publishing_information on delete cascade on update cascade," "constraint fk_bi0 foreign key (publishing) references publishing_information on delete cascade on update cascade,"
@ -65,7 +65,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists groups_records(" q.prepare("create table if not exists groups_records("
"record_id integer auto_increment primary key," "record_id integer primary key autoincrement,"
"name text not null)"); "name text not null)");
if (!q.exec()) { if (!q.exec()) {
qDebug() << q.lastError(); qDebug() << q.lastError();
@ -73,7 +73,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists labels_records( " q.prepare("create table if not exists labels_records( "
"record_id integer auto_increment primary key, " "record_id integer primary key autoincrement, "
"name text not null, describe text)"); "name text not null, describe text)");
if (!q.exec()) { if (!q.exec()) {
qDebug() << q.lastError(); qDebug() << q.lastError();
@ -81,7 +81,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists books_manage(" q.prepare("create table if not exists books_manage("
"record_id integer auto_increment primary key," "record_id integer primary key autoincrement,"
"book integer not null,`group` integer not null," "book integer not null,`group` integer not null,"
"constraint fk_bm0 foreign key (book) references books_information(record_id) on delete cascade on update cascade," "constraint fk_bm0 foreign key (book) references books_information(record_id) on delete cascade on update cascade,"
"constraint fk_bm1 foreign key (`group`) references groups_records(record_id) on delete cascade on update cascade)"); "constraint fk_bm1 foreign key (`group`) references groups_records(record_id) on delete cascade on update cascade)");
@ -91,7 +91,7 @@ DBUnit::init_basic_tables(QSqlDatabase& db_ins)
} }
q.prepare("create table if not exists book_labels_manage( " q.prepare("create table if not exists book_labels_manage( "
"record_id integer auto_increment primary key, " "record_id integer primary key autoincrement, "
"book integer not null,label integer not null, " "book integer not null,label integer not null, "
"constraint fk_blm0 foreign key (book) references books_information(book_id) on delete cascade on update cascade," "constraint fk_blm0 foreign key (book) references books_information(book_id) on delete cascade on update cascade,"
"constraint fk_blm1 foreign key (label) references labels_records(record_id) on delete cascade on update cascade)"); "constraint fk_blm1 foreign key (label) references labels_records(record_id) on delete cascade on update cascade)");

119
labelsmanagement.cpp Normal file
View File

@ -0,0 +1,119 @@
#include "labelsmanagement.h"
#include "dbunit.h"
#include <QDebug>
#include <QGridLayout>
#include <QInputDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QSqlError>
#include <QSqlQuery>
LabelsManagement::LabelsManagement(DBUnit* tool, QWidget* parent)
: QDialog(parent), db_host(tool), keywords_input(new QLineEdit(this)),
labels_view(new QTableView(this)), labels_model(new QStandardItemModel(this)),
append(new QPushButton("增加", this)), remove(new QPushButton("移除", this))
{
auto layout = new QGridLayout(this);
layout->addWidget(keywords_input, 0, 0, 1, 4);
layout->addWidget(labels_view, 1, 0, 3, 4);
layout->addWidget(append, 4, 0, 1, 2);
layout->addWidget(remove, 4, 2, 1, 2);
labels_view->setModel(labels_model);
labels_model->setHorizontalHeaderLabels(QStringList() << "名称"
<< "简述");
init_labels_model(this->labels_model);
connect(append, &QPushButton::clicked, [this]() {
QInputDialog in;
in.setWindowTitle("输入标签名称");
in.setLabelText("输入重复标签:");
if (in.exec() == QDialog::Rejected)
return;
auto q = db_host->getQuery();
q.prepare("insert into labels_records "
"(name, describe) values(:n, :d)");
q.bindValue(":n", in.textValue());
q.bindValue(":d", "请手动修改简介文字");
if (!q.exec()) {
qDebug() << q.lastError();
return;
}
q.prepare("select record_id from labels_records order by record_id desc");
if (!q.exec()) {
qDebug() << q.lastError();
return;
}
QList<QStandardItem*> row;
row << new QStandardItem(in.textValue());
row.last()->setData(q.value(0), Qt::UserRole + 1);
row << new QStandardItem("请手动修改标签简介");
labels_model->appendRow(row);
});
connect(remove, &QPushButton::clicked, [this]() {
auto index = labels_view->currentIndex();
if (!index.isValid())
return;
auto idindex = index.sibling(index.row(), 0);
auto q = db_host->getQuery();
q.prepare("delete from labels_records where record_id=:id");
q.bindValue(":id", idindex.data(Qt::UserRole + 1));
if (!q.exec()) {
qDebug() << q.lastError();
return;
}
labels_model->removeRow(index.row());
});
connect(keywords_input, &QLineEdit::textChanged, this, &LabelsManagement::input_query);
}
void
LabelsManagement::init_labels_model(QStandardItemModel* model)
{
auto q = db_host->getQuery();
q.prepare("select record_id, name, describe from labels_records");
if (!q.exec()) {
qDebug() << q.lastError();
return;
}
model->removeRows(0, model->rowCount());
while (q.next()) {
QList<QStandardItem*> row;
row << new QStandardItem(q.value(1).toString());
row.last()->setData(q.value(0), Qt::UserRole + 1);
row << new QStandardItem(q.value(2).toString());
model->appendRow(row);
}
}
void
LabelsManagement::input_query(const QString& keywords)
{
labels_model->removeRows(0, labels_model->rowCount());
if (keywords.trimmed().isEmpty())
return;
QString string = "select record_id, name, describe from labels_records ";
if (keywords != "*")
string += "where name like '%" + keywords + "%'";
auto q = db_host->getQuery();
q.prepare(string);
if (!q.exec()) {
qDebug() << q.lastError();
return;
}
while (q.next()) {
QList<QStandardItem*> row;
row << new QStandardItem(q.value(1).toString());
row.last()->setData(q.value(0), Qt::UserRole + 1);
row << new QStandardItem(q.value(2).toString());
labels_model->appendRow(row);
}
}

30
labelsmanagement.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef LABELSMANAGEMENT_H
#define LABELSMANAGEMENT_H
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QStandardItemModel>
#include <QTableView>
#include <QWidget>
class DBUnit;
class LabelsManagement : public QDialog {
Q_OBJECT
public:
explicit LabelsManagement(DBUnit* tool, QWidget* parent = nullptr);
private:
DBUnit* const db_host;
QLineEdit* const keywords_input;
QTableView* const labels_view;
QStandardItemModel* const labels_model;
QPushButton *const append, *const remove;
void init_labels_model(QStandardItemModel* model);
void input_query(const QString& keywords);
};
#endif // LABELSMANAGEMENT_H

View File

@ -1,14 +1,17 @@
#include "dbunit.h" #include "dbunit.h"
#include "labelsmanagement.h"
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
MainWindow w; // MainWindow w;
w.show(); // w.show();
// DBUnit t; DBUnit tool;
LabelsManagement m(&tool);
m.show();
return a.exec(); return a.exec();
} }