在C ++ QT上执行sql后应用程序崩溃

时间:2017-06-17 14:08:07

标签: c++ mysql database qt

我刚刚学习了c ++如何在QT上工作,而iam在执行查询时遇到问题,就像我在我的exe上看到的那样,当我按下按钮在我的数据库中插入一些字段时,我得到应用程序崩溃的错误。

我有一个学生数据库和4个字段(id,name,lastname,studentid) 我使用4rth来搜索数据库或删除学生,因为第一个是自动增量。

我允许用户使用编辑字段将学生添加到数据库中,当他按下按钮时,我会在学生表中插入值。

当我按下按钮时,我得到一个崩溃错误。

Database.cpp

#include "database.h"
# include <QVariant>
# include <QDebug>
Database::Database()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("students.db");
    db.open();
    QSqlQuery q(db);
    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)"));
}


void    Database::insertStudent(Student &s)
{
QSqlQuery query(db);
query.prepare("INSERT INTO student (name,lastname,semester,studentid) VALUES (:name,:lastname,:semester,:studentid)");
query.bindValue(":name", s.getname());
query.bindValue(":lastname",s.getlastname());
query.bindValue(":semester", s.getsemester());
query.bindValue(":studentid",s.getstudentid());
query.exec();

}

QVector<Student> Database::students()
{
    QVector<Student> s;
    QSqlQuery query(db);
    query.exec("SELECT name,lastname,semester,studentid FROM students");
    while (query.next())
    {
        Student C(query.value(0).toString(),query.value(1).toString(),query.value(2).toInt(),query.value(3).toInt());
        s.append(C);
    }
    return s;
}

Student  Database::getStudent(int id,bool &found)
{
 QSqlQuery query(db);
 query.exec("SELECT name,lastname,semester,studentid FROM student where studentid="+QString::number(id));
 if(query.next()==true)
 {
     Student C(query.value(0).toString(),query.value(1).toString(),query.value(2).toInt(),query.value(3).toInt());
     found=true;
     return C;
 }
 else found=false;
}

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

Database::~Database()
{
    db.close();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setFixedSize(600,300);
    setWindowTitle("Test Sql");
    mainWidget=new QWidget;
    setCentralWidget(mainWidget);
    mainWidget->setFixedSize(this->width(),this->height());
    mainLayout=new QVBoxLayout;
    mainWidget->setLayout(mainLayout);
    mydb=NULL;
    makeMenus();
    panel=new QStackedLayout;
    mainLayout->addLayout(panel);
    makeInputForm();
    makeDisplayForm();
    makeSearchForm();
}

void    MainWindow::makeMenus()
{

    editMenu=new QMenu("EDIT");
    editMenu->addAction("NEW");
    editMenu->addAction("DISPLAY");//tha exei kai pliktra update kai delete
    editMenu->addAction("SEARCH");
    connect(editMenu,SIGNAL(triggered(QAction*)),this,SLOT(editSlot(QAction*)));
    menuBar()->addMenu(editMenu);
}


void    MainWindow::makeInputForm()
{
   QWidget *tab1=new QWidget;
   panel->addWidget(tab1);
   tab1->setFixedSize(95*this->size().width()/100,this->size().height()/2);
   QVBoxLayout *tab1layout=new QVBoxLayout;
   tab1->setLayout(tab1layout);
   QHBoxLayout *line1=new QHBoxLayout;
   tab1layout->addLayout(line1);
   QHBoxLayout *line2=new QHBoxLayout;
   tab1layout->addLayout(line2);
   nameEdit=new QLineEdit;
   nameEdit->setPlaceholderText("NAME");
   line1->addWidget(nameEdit);
   lastnameEdit=new QLineEdit;
   lastnameEdit->setPlaceholderText("LASTNAME");
   semesterEdit=new QLineEdit;
   semesterEdit->setPlaceholderText("SEMESTER");

   line1->addWidget(lastnameEdit);
   line1->addWidget(semesterEdit);
   idEdit=new QLineEdit;
   idEdit->setPlaceholderText("ID");
   idEdit->setValidator(new QIntValidator(0,10000));
   line1->addWidget(idEdit);
   addButton=new QPushButton;
   addButton->setText("ADD PERSON");
   connect(addButton,SIGNAL(clicked(bool)),this,SLOT(addSlot()));

   line2->addWidget(addButton);
   clearButton=new QPushButton;
   clearButton->setText("CLEAR");
   connect(clearButton,SIGNAL(clicked(bool)),this,SLOT(clearSlot()));
   line2->addWidget(clearButton);
}

void    MainWindow::addSlot()
{

    if(nameEdit->text().isEmpty() || lastnameEdit->text().isEmpty() ||
            idEdit->text().isEmpty())
    {
        QMessageBox::critical(this,"Error","Empty fields");
        return;
    }
    Student S(nameEdit->text(),lastnameEdit->text(),semesterEdit->text().toInt(),idEdit->text().toInt());
    mydb->insertStudent(S);
    reloadTable();
}

database.h

#ifndef DATABASE_H
#define DATABASE_H
# include <QSqlDatabase>
# include <QSqlQuery>
# include <QSqlError>
# include "student.h"
class Database
{
private:
    QSqlDatabase db;
public:
    Database();
    QVector<Student> students();
    Student  getStudent(int id,bool &found);
    void insertStudent(Student &s);
    QVector<Student*> getStudents();
    void    deleteStudent(int id);
    ~Database();
};

#endif // DATABASE_H

student.h

#ifndef STUDENT_H
#define STUDENT_H

# include <QString>
class Student
{
private:
    QString name,lastname;
    int studentid,semester;
public:
    Student();
    Student(QString n,QString l,int s,int p);
    QString getname();
    QString getlastname();
    int getstudentid();
    int getsemester();
    QString toString();
};


#endif // STUDENT_H

1 个答案:

答案 0 :(得分:0)

解释:在 MainWindow 类的构造函数中,将指针 mydb 初始化为 NULL 。然后在插槽MainWindow::addSlot()中调用 mydb 所指向的对象的方法Database::insertStudent(Student &s),因为它是 NULL ,程序崩溃。< / p>

解决方案MainWindow::MainWindow(QWidget *parent)使用mydb=NULL;替换mydb = new Database();