Can't write into database
-
First things first, as you have - as far as I can tell - two separate problems here.
First: with each press of the button you add a database connection (so the second time you press it you try to set new connection to db, so the old one is removed, so you get the info). Then each time you create table which might as well exist.
To avoid that I'd suggest moving the addDatabase and table creation somewhere else, where it is called once. You can retrieve database with:auto db=QSqlDatabase::database(); if (!db.isOpen()) { db.Open(); }
and then continue.
Also, QSqlQuery returns lastError() and lastError().text() might be useful to debug the execution result.wrote on 5 Mar 2020, 16:11 last edited by@artwaw thanks you. I moved my database and tableCreation in my main function. I assume that it's called only once then. It still doesn't work. By printing lastError().txt i get the following message:
SqlQuery::prepare: database not open
"Driver not loaded Driver not loaded" -
@artwaw thanks you. I moved my database and tableCreation in my main function. I assume that it's called only once then. It still doesn't work. By printing lastError().txt i get the following message:
SqlQuery::prepare: database not open
"Driver not loaded Driver not loaded" -
@Babs Ok. So now you can test db.open() - it returns a value. On false, error should be present in lastError().
-
@artwaw it returns false and the error driver not loaded. I do the verification:
if(!db.isOpen()){ db.open(); qDebug()<<db.lastError().text(); }
wrote on 5 Mar 2020, 16:25 last edited by@Babs Are you completely sure that addDatabase() has been called at any point before call do QSqlDatabase::database()?
You could also test if driver is available by using QSqlDatabase::drivers() and other methods described in documentation. -
@Babs Are you completely sure that addDatabase() has been called at any point before call do QSqlDatabase::database()?
You could also test if driver is available by using QSqlDatabase::drivers() and other methods described in documentation. -
@Babs Are you completely sure that addDatabase() has been called at any point before call do QSqlDatabase::database()?
You could also test if driver is available by using QSqlDatabase::drivers() and other methods described in documentation. -
wrote on 5 Mar 2020, 16:47 last edited by
@artwaw i'm working with QBS. When i don't specify the connectionName in addDataBase() function it open. I have the error "number of parameters incorrect". I think i don't have the good parameter types. I defined a QString as a TEXT is SQLite. Is it correct?
-
wrote on 5 Mar 2020, 16:47 last edited by
@artwaw here is my QBS file
import qbs Project{ name: "PlanningModrian" Product{ files: [ ] name: ( "modrian"); type: "application"; targetName: (project.targetPrefixTools + "modrian"); cpp.dynamicLibraries: { var ret = []; if (Qt.core.versionMinor < 4) { // NOTE : prior to Qt5.4 we had to link libGL manually ret.push ("GL"); } return ret; } cpp.cxxLanguageVersion: "c++14" readonly property stringList qmlImportPaths : [sourceDirectory + "/import"]; // equivalent to QML_IMPORT_PATH += $$PWD/import Depends { name: "Qt.quick" } Depends { name: "Qt"; submodules: ["core", "gui", "qml", "quick","testlib","sql"]; } Group{ name: "C++ sources"; files: [ "planning.cpp", "main.cpp", "admin.cpp", "testPlanning.cpp", "user.cpp", "equipement.cpp", "equimenttablemodel.cpp", "palier.cpp", "paliermodel.cpp", "appwrapper.cpp", ] } Group { qbs.install: (product.type === "dynamiclibrary"); fileTagsFilter: product.type; } Group{ name: "C++ headers"; files: [ "admin.h", "appwrapper.h", "datetimevalidator.cpp", "datetimevalidator.h", "equimenttablemodel.h", "manager.cpp", "manager.h", "paliermodel.h", "planning.h", "user.h", "equipement.h", "palier.h", ] } Group { name: "QML files" files: [ "DatePicker.qml", "DateUtils.js", "Planning.qml", "main.qml", "PagePlannification.qml", "qml.qrc", ] } //Depends { name: (project.namePrefixBase + "utils"); } Depends { name: "libqtqmltricks-qtsupermacros"; } Depends { name: "libqtqmltricks-qtqmlmodels"; } Depends { name: "libqtqmltricks-qtquickuielements"; } } }
-
@artwaw here is my QBS file
import qbs Project{ name: "PlanningModrian" Product{ files: [ ] name: ( "modrian"); type: "application"; targetName: (project.targetPrefixTools + "modrian"); cpp.dynamicLibraries: { var ret = []; if (Qt.core.versionMinor < 4) { // NOTE : prior to Qt5.4 we had to link libGL manually ret.push ("GL"); } return ret; } cpp.cxxLanguageVersion: "c++14" readonly property stringList qmlImportPaths : [sourceDirectory + "/import"]; // equivalent to QML_IMPORT_PATH += $$PWD/import Depends { name: "Qt.quick" } Depends { name: "Qt"; submodules: ["core", "gui", "qml", "quick","testlib","sql"]; } Group{ name: "C++ sources"; files: [ "planning.cpp", "main.cpp", "admin.cpp", "testPlanning.cpp", "user.cpp", "equipement.cpp", "equimenttablemodel.cpp", "palier.cpp", "paliermodel.cpp", "appwrapper.cpp", ] } Group { qbs.install: (product.type === "dynamiclibrary"); fileTagsFilter: product.type; } Group{ name: "C++ headers"; files: [ "admin.h", "appwrapper.h", "datetimevalidator.cpp", "datetimevalidator.h", "equimenttablemodel.h", "manager.cpp", "manager.h", "paliermodel.h", "planning.h", "user.h", "equipement.h", "palier.h", ] } Group { name: "QML files" files: [ "DatePicker.qml", "DateUtils.js", "Planning.qml", "main.qml", "PagePlannification.qml", "qml.qrc", ] } //Depends { name: (project.namePrefixBase + "utils"); } Depends { name: "libqtqmltricks-qtsupermacros"; } Depends { name: "libqtqmltricks-qtqmlmodels"; } Depends { name: "libqtqmltricks-qtquickuielements"; } } }
-
@artwaw here is my QBS file
import qbs Project{ name: "PlanningModrian" Product{ files: [ ] name: ( "modrian"); type: "application"; targetName: (project.targetPrefixTools + "modrian"); cpp.dynamicLibraries: { var ret = []; if (Qt.core.versionMinor < 4) { // NOTE : prior to Qt5.4 we had to link libGL manually ret.push ("GL"); } return ret; } cpp.cxxLanguageVersion: "c++14" readonly property stringList qmlImportPaths : [sourceDirectory + "/import"]; // equivalent to QML_IMPORT_PATH += $$PWD/import Depends { name: "Qt.quick" } Depends { name: "Qt"; submodules: ["core", "gui", "qml", "quick","testlib","sql"]; } Group{ name: "C++ sources"; files: [ "planning.cpp", "main.cpp", "admin.cpp", "testPlanning.cpp", "user.cpp", "equipement.cpp", "equimenttablemodel.cpp", "palier.cpp", "paliermodel.cpp", "appwrapper.cpp", ] } Group { qbs.install: (product.type === "dynamiclibrary"); fileTagsFilter: product.type; } Group{ name: "C++ headers"; files: [ "admin.h", "appwrapper.h", "datetimevalidator.cpp", "datetimevalidator.h", "equimenttablemodel.h", "manager.cpp", "manager.h", "paliermodel.h", "planning.h", "user.h", "equipement.h", "palier.h", ] } Group { name: "QML files" files: [ "DatePicker.qml", "DateUtils.js", "Planning.qml", "main.qml", "PagePlannification.qml", "qml.qrc", ] } //Depends { name: (project.namePrefixBase + "utils"); } Depends { name: "libqtqmltricks-qtsupermacros"; } Depends { name: "libqtqmltricks-qtqmlmodels"; } Depends { name: "libqtqmltricks-qtquickuielements"; } } }
wrote on 5 Mar 2020, 16:58 last edited byI resolved my problem. I made an error in the type an name of one of the parameters. Thanks for your help @artwaw.
-
I resolved my problem. I made an error in the type an name of one of the parameters. Thanks for your help @artwaw.
12/14