首页 > 解决方案 > 插入的数据未保存 Qt

问题描述

我的问题是我正在尝试将数据保存到项目中的数据库中。它在操作后立即显示,但在我关闭应用程序并再次打开后,我找不到数据。这是我插入新行的代码:

void MainWindow::on_pushButtonUlozit_clicked(){   QMessageBox::StandardButton reply=QMessageBox::question(this,"Ukladání","Přejete si data uložit?", QMessageBox::Yes | QMessageBox::No);
if (reply==QMessageBox::Yes) {QSqlQuery qrry;
if(ui->radioButtonWeb->isChecked()){

    QString kod = ui->kodLine->text();
    qDebug()<<kod;
    QString odkaz = ui->odkazLine->text();
    qDebug()<<odkaz;
    QString popis = ui->popisLine->text();
    qDebug()<<popis;
    QString jazyk = ui->jazykLine->text();
    qDebug()<<jazyk;
    qrry.prepare( "INSERT INTO web (kod_predmet, odkaz, popis, kod_jazyk) VALUES ('"+kod+"','"+odkaz+"','"+popis+"','"+jazyk+"');");//ulozit data do tabulky 'web'
}
else if (ui->radioButtonKniha->isChecked()){
    QString kod1 = ui->kodLine1->text();
    qDebug()<<kod1;
    QString isbn = ui->isbnLine->text();
    qDebug()<<isbn;
    QString autor = ui->autorLine->text();
    qDebug()<<autor;
    QString nazev = ui->nazevLine->text();
    qDebug()<<nazev;
    QString vydat = ui->vydatEdit->text();
    qDebug()<<vydat;
    QString jazyk = ui->jazykLine1->text();
    qDebug()<<jazyk;
    QString rok = ui->rokLine->text();
    qDebug()<<rok;
    QString insert = "INSERT INTO literatura (kod_predmet, ISBN, autor, nazev, rok, vydavatelstvi,kod_jazyk) VALUES ('"+kod1+"','"+isbn+"','"+autor+"','"+nazev+"','"+rok+"','"+vydat+"','"+jazyk+"');";//ulozit data do tabulky 'literatura'
}
if (qrry.exec()) {
    QMessageBox::information(this,tr("Ukladání"),tr("Uloženo"));
} else
{
    QMessageBox::critical(this,tr("error::"),tr("Chyba!"));
}}
else {
    QMessageBox::information(this,"Ukladání","Data nebyla uložena");
}

}

插入数据

更改后表格的外观

在我关闭并重新打开我的项目后,该行丢失了。简化代码:

QSqlQuery qrry;
    QString kod = ui->kodLine->text();
    qDebug()<<kod;
    QString odkaz = ui->odkazLine->text();
    qDebug()<<odkaz;
    QString popis = ui->popisLine->text();
    qDebug()<<popis;
    QString jazyk = ui->jazykLine->text();
    qDebug()<<jazyk;
    qrry.prepare( "INSERT INTO web (kod_predmet, odkaz, popis, kod_jazyk) VALUES ('"+kod+"','"+odkaz+"','"+popis+"','"+jazyk+"');");//ulozit data do tabulky 'web'
    qrry.exec();

这是 dbliteratura.h 中与我的数据库的连接:

inline void dbliteratura(){
QString sqlfile = "../dbliteratura.sql";
QFile   f(sqlfile);
if (!f.open(QIODevice::ReadOnly))
    {
        QMessageBox::critical(0, "IO Error", "Couldn't open file" + sqlfile + " for reading");
        exit(1);
    }

QSqlDatabase db = QSqlDatabase::database();
if (!db.isOpen())
    {
        QMessageBox::critical(0, "Database Error", "Database is not open");
        exit(2);
    }

// je to divne, ale neni mozne zpracovat celou sql davku, jednotlive prikazy
// tedy musime oddelit (;) a zadavat postupne
QString sql;
QSqlQuery q;
char z;

db.transaction();
while ( f.getChar(&z) )
    {
        sql = z;
        while (f.getChar(&z) && (z != ';'))  sql.push_back(z);
        q.exec(sql);
    }
db.commit();}

和 MainWindow.cpp:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);
this->setWindowTitle("DB Literatura");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
dbliteratura();

标签: sqlqt

解决方案


推荐阅读