sql - 插入的数据未保存 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();
解决方案
推荐阅读
- acumatica - Acumatica 和 Outlook 插件和 Azure 单点登录错误
- python - 根据管理员批准的帖子过滤 Django 中的博客帖子
- bash - Chrome 应用程序窗口的 PID 正在更改
- 3d - 显着减小 .obj 文件的大小
- c++ - C++ 和 EXE 无法运行
- reactjs - 即使我已经使用 React Routing 进行了身份验证,我的登录表单也会显示?
- javascript - 实时更新烧瓶中的流数据
- java - 字段 authenticationManager LoginController 需要一个无法找到的 typeAuthenticationManager' bean
- ios - 如何将多个 nib 文件与多个数组一起使用?
- python - 胜负球队的积分总和?