Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you help me fix my books.cpp so the function to add books is complete. the picture is the books.h and heres the books.cpp .

Could you help me fix my books.cpp so the function to add books is complete. the picture is the books.h and heres the books.cpp.( could you make a database) please
#include "books.h"
#include
#include
#include
#include
#include
books::books(QWidget *parent) : QMainWindow(parent){
setWindowTitle("Library Books");
// Central widget and layout
QWidget *centralWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
// Book search list (QTreeView)
bookSearchList = new QTreeView(this);
layout->addWidget(bookSearchList);
setCentralWidget(centralWidget);
// Populate the book search list from the database
populateBookSearchList();
}
void books::populateBookSearchList(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("your_database.db");
if (!db.open()){
qDebug() "Unable to open database";
return;
}
QSqlQuery query;
if (query.exec("SELECT * FROM library.books ORDER BY genre")){
QStandardItemModel *model = new QStandardItemModel(this);
QString currentGenre;
QStandardItem *currentGenreItem = nullptr;
while (query.next()){
QString genre = query.value("genre").toString();
if (genre != currentGenre){
// New genre, create a new parent item for it
currentGenre = genre;
currentGenreItem = new QStandardItem(genre);
model->appendRow(currentGenreItem);
}
// Create a child item for each book in the genre
QList bookItems;
bookItems new QStandardItem(query.value("book_name").toString());
bookItems new QStandardItem(query.value("author").toString()); // Author
bookItems new QStandardItem(query.value("published").toString()); // Published date
currentGenreItem->appendRow(bookItems);
}
// Set the model to the book search list
ui->bookSearchList->setModel(model);
ui->bookSearchList_2->setModel(model);
} else {
qDebug() "Query failed:" query.lastError().text();
}
}
void books::addBookToDatabase(){
QString bookId = ui->bookid->text();
QString name = ui->name->text();
QString author = ui->author->text();
QString genre = ui->genre->text();
QString publish = ui->publish->text();
QString status = ui->status->currentText();
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query(db);
query.prepare("INSERT INTO library.books (book_id, book_name, author, genre, published, book_health)"
"VALUES (:book_id, :book_name, :author, :genre, :published, :book_health)");
query.bindValue(":book_id", bookId);
query.bindValue(":book_name", name);
query.bindValue(":author", author);
query.bindValue(":genre", genre);
query.bindValue(":published", publish);
query.bindValue(":book_health", status);
if (query.exec()){
qDebug() "Book added successfully";
// Update the book search list after adding the new book
populateBookSearchList();
} else {
qDebug() "Error adding book:" query.lastError().text();
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

What is meant by the term industrial relations?

Answered: 1 week ago