Qt连接mysql数据库,执行QSQLQuery执行语句的时候程序崩溃
我现在用qt做的一个只连接数据库的简单的项目,只是一个查询dialog.cpp和插入数据insert.cpp的两个界面,把数据库单独新建的一个类,用的mysql。现在想从表中查一个数据,可是每次执行dialog的QSqlQuery的执行语句的时候程序就崩溃了,看原因应该是内存溢出了,可是昨天我测试的时候是可以查出来的,而且插入数据是可以的,sql语句也没有问题,是哪里出了问题?!dialog是查询的,insert是插入数据的
database.h:
#ifndef DATABASE_H
#define DATABASE_H
#include <QSqlDatabase>
#include <QtSql>
#include <QtCore>
class Database : public QSqlDatabase
{
public:
Database();
QSqlDatabase db;
};
#endif // DATABASE_H
database.cpp:
#include "database.h"
Database::Database()
{
db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("localhost");
db.setDatabaseName("mydsn_32");
if(db.open())
{
qDebug() << "database opened!";
}
else
{
qDebug() << "Error: " <<db.lastError().text();
}
}
dialog.h:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QtCore>
#include <QMessageBox>
#include "database.h"
#include "insert.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
bool checkPerson(QString);
bool checkName(QString);
Database tdb;
private slots:
void on_yes_clicked();
void on_no_clicked();
void on_pushButton_clicked();
private:
Ui::Dialog *ui;
QString person;
QString name;
QString sql_person;
QString sql_name;
QSqlQuery qry_person;
QSqlQuery qry_name;
Insert *myinsert;
};
#endif // DIALOG_H
dialog.cpp:
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
sql_person = "select person from test1";
sql_name = "select name from test1 where person = :name" ;
myinsert = new Insert(this);
tdb.db.open();
}
Dialog::~Dialog()
{
delete ui;
tdb.db.close();
myinsert->deleteLater();
}
void Dialog::on_yes_clicked()
{
person = ui->lineEdit_person->text();
name = ui->lineEdit_name->text();
if(NULL != name || NULL != person)
{
qDebug() << "1";
if(checkPerson(person))//人存在
{
QMessageBox::information(this, "ok", "ok");
if(checkName(name))//名字对应
{
QMessageBox::information(this, "ok", "ok");
}
else
{
QMessageBox::information(this, "no name", "no name");
}
}
else
{
QMessageBox::information(this, "no person", "no person");
}
}
else
{
return;
}
}
void Dialog::on_no_clicked()
{
this->close();
}
bool Dialog::checkPerson(QString userperson)
{
//check person if exsit
qDebug() << "2";
if(qry_person.exec(sql_person))
{
while(qry_person.next())
{
if(qry_person.value(0).toString() == userperson)
{
return true;
}
};
return false;
}
}
bool Dialog::checkName(QString username)
{
qry_name.prepare(sql_name);
qry_name.bindValue("name", person);
if(qry_name.exec())
{
while(qry_name.next())
{
if(qry_name.value(0).toString() == username)
{
return true;
}
};
return false;
}
}
void Dialog::on_pushButton_clicked()
{
myinsert->show();
}
insert.h:
#ifndef INSERT_H
#define INSERT_H
#include <QDialog>
#include "database.h"
#include <QtCore>
#include <QMessageBox>
namespace Ui {
class Insert;
}
class Insert : public QDialog
{
Q_OBJECT
public:
explicit Insert(QWidget *parent = 0);
~Insert();
Database tdb;
bool checkUnq(QString);
bool insertCheck(QString, QString);
private slots:
void on_insert_yes_clicked();
void on_insert_no_clicked();
private:
Ui::Insert *ui;
QString insert_person;
QString insert_name;
QString sql_insert;
QString sql_select;
QSqlQuery qry_insert;
QSqlQuery qry_select;
};
#endif // INSERT_H
insert.cpp:
#include "insert.h"
#include "ui_insert.h"
Insert::Insert(QWidget *parent) :
QDialog(parent),
ui(new Ui::Insert)
{
ui->setupUi(this);
sql_insert = "insert into test1(person, name) values(:person, :name)";
sql_select = "select person from test1";
tdb.db.open();
}
Insert::~Insert()
{
delete ui;
tdb.db.close();
}
void Insert::on_insert_yes_clicked()
{
insert_person = ui->insert_person->text();
insert_name = ui->insert_name->text();
if(NULL != insert_person || NULL != insert_name)
{
if(checkUnq(insert_person))
{
insertCheck(insert_person, insert_name);
QMessageBox::information(this, "ok", "ok");
}
else
{
QMessageBox::information(this, "not only", "not only");
}
}
else
{
return;
}
}
void Insert::on_insert_no_clicked()
{
this->close();
}
bool Insert::checkUnq(QString userperson)
{
//查看是否唯一
if(qry_select.exec(sql_select))
{
while(qry_select.next())
{
if(qry_select.value(0).toString() == userperson)
{
return false;
}
};
return true;
}
}
bool Insert::insertCheck(QString userperson, QString username)
{
//insert data
qry_insert.prepare(sql_insert);
qry_insert.bindValue(":person", userperson);
qry_insert.bindValue(":name", username);
if(qry_insert.exec())
{
return true;
}
return false;
}