pfoutzop
pfoutzop

Reputation: 63

Trying to delete from database on QT table

I have a database named student with 4 fields (name , lastname, semester and studentid).

I have an application connected that lets the user inserts students in the database having an option to show them on a table after the inserts.

Table has a DELETE button for every insert-student which I have connected to separate function to let the user having the option to delete individually students based on the studentid.

Problem is that I seem to do something wrong as the delete button doesn't work when its clicked. I get no error though so I need your help finding the bug.

Database created code:

Database::Database()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("students.db");
    db.open();
    QSqlQuery q(db);
    q.exec(QString("DROP TABLE student;"));

    q.exec(QString("create table if not exists student(")+
                        QString("id integer primary key autoincrement,")+
                        QString("name varchar(1024),lastname varchar(1024),semester integer,")+
                        QString("studentid integer)"));
}

Delete from database called in database.cpp

void    Database::deleteStudent(int id)
{
    QSqlQuery query(db);
    query.exec("DELETE student where studentid="+QString::number(id));
}

MainWindow.cpp

//Creating the Table-LIST of the inserts

    void    MainWindow::makeDisplayForm()
    {
        QWidget *tab2=new QWidget;
        panel->addWidget(tab2);
        tab2->setFixedSize(95*this->size().width()/100,this->size().height()/2);
        QVBoxLayout *tab2layout=new QVBoxLayout;
        tab2->setLayout(tab2layout);
        table=new QTableWidget;
        table->setRowCount(1);
        table->setColumnCount(5);
        QStringList header;
        header<<"NAME"<<"LASTNAME"<<"SEMESTER"<<"ID"<<"DELETE";
        table->setHorizontalHeaderLabels(header);
        tab2layout->addWidget(table);
    }

void    MainWindow::reloadTable()
{
    QVector<Student> p=mydb->students();
    table->clearContents();
    table->setRowCount(p.size());
    for(int i=0;i<p.size();i++)
    {
        Student pt=p[i];
        QLineEdit *tableName=new QLineEdit;
        tableName->setText(pt.getname());
        table->setCellWidget(i,0,tableName);

        QLineEdit *tableLastName=new QLineEdit;
        tableLastName->setText(pt.getlastname());
        table->setCellWidget(i,1,tableLastName);


        table->setItem(i,2,new QTableWidgetItem(QString::number(pt.getsemester())));
        table->setItem(i,3,new QTableWidgetItem(QString::number(pt.getstudentid())));

        //DELETE BUTTON CREATE
        QPushButton *deleteButton=new QPushButton;
        deleteButton->setText("DELETE");
        connect(deleteButton,SIGNAL(clicked(bool)),this,SLOT(deleteSlot()));
        deleteButton->setProperty("STUDENTID",pt.getstudentid());
        table->setCellWidget(i,4,deleteButton);

    }
}

//DELETE button connect function.

void    MainWindow::deleteSlot()
{
    QPushButton *b=(QPushButton *)sender();
    int studtid=b->property("STUDENTID").toInt();
    mydb->deleteStudent(studtid);
    reloadTable();
}

//Reload table is called after the delete function in order to show the new database without the deleted student.

Student.cpp

#include "student.h"

Student::Student()
{
    name="";
    lastname="";
    studentid=0;
    semester=0;
}

Student::Student(QString n,QString l,int s,int p)
{
    name=n;
    lastname=l;
    semester=s;
    studentid=p;
}

int Student::getstudentid()
{
    return studentid;
}

UPDATE: if I change void Database::deleteStudent(int id)query to:

query.exec(QString("DROP TABLE student;"));

Table is deleted just fine after the delete button so its something wrong with studentid or the query? I need to delete individual inserts not all the table.

Upvotes: 0

Views: 2170

Answers (1)

klocur
klocur

Reputation: 24

Try this in your function:

DELETE FROM student WHERE studentid=....

Upvotes: 1

Related Questions