I have a project that I transfer from Visual Studio to QtCreator. The compiler changes accordingly. And now the problem with working with the database. In the studio, it all worked, but QtCreator and MinGW do not like what I don’t understand. Specifically, the problem boils down to the fact that when processing a SQL query it does not see the table. If you look through SQLiteManager, then all tables are in place and the query is executed and gives the desired output.
The problem is here:
QVector<QStringList> DbManager::showNotes() { if (MattyNotesDb.isOpen()) { QVector<QStringList> VectorOfNotes; QueryConstructor SelectAll; SelectAll.setTableName(QStringLiteral("Notes")); SelectAll.setOrderByClause("NoteId", Descending); QSqlQuery getNotesQuery; if( getNotesQuery.exec(SelectAll.constructSelectQuery())) // exec возвращает false { while (getNotesQuery.next()) // соответственно тоже возвращает false { QStringList Fields; for (int i = 0;i < 9;i++) { Fields.push_back(getNotesQuery.value(i).toString()); } VectorOfNotes.push_back(Fields); } } else { QMessageBox::critical(NULL, QObject::tr("Error"), getNotesQuery.lastError().text()); // no such table. Unable to execute statement } return VectorOfNotes; } else { showIsNotOpenError(); return QVector<QStringList>(); } } Connect to the database:
bool DbManager::connect(const QString & path) { MattyNotesDb = QSqlDatabase::addDatabase("QSQLITE"); MattyNotesDb.setDatabaseName(path); // path="MattyNotes.sqlite" if(QFile::exists(path)) // true { if (!MattyNotesDb.open()) // open=true, то есть if(false) { QMessageBox::critical(NULL, QObject::tr("Error"), MattyNotesDb.lastError().text()); MattyNotesDb.close(); return false; } return true; } else { return false; } } Making a request:
QString QueryConstructor::constructSelectQuery() { QString ResultQuery = ""; if (TableName != "") { ResultQuery.append("SELECT "); if (WhatToSelectFieldNames.isEmpty()) { ResultQuery.append("*"); } else { for (int i = 0;i < WhatToSelectFieldNames.length()-1;i++) { ResultQuery.append(" " + WhatToSelectFieldNames[i] + ","); } ResultQuery.append(WhatToSelectFieldNames.last()); } ResultQuery.append(" FROM " + TableName + constructWhereEqualsClause() + " " + OrderByClause); } return ResultQuery; // "SELECT * FROM Notes ORDER BY NoteId DESC; " } UPD: Problem in file path. If you manually set the full path, then everything works.
one.
QString PathToDb = QCoreApplication::applicationDirPath() + "/MattyNotes.sqlite"; This option eventually sets the PathToDb variable to the value "C: / Users / Matty / Documents / QtCreator / build-MattyNotes-Desktop_Qt_5_7_1_MinGW_32bit-Debug / debug / MattyNo" The folder is correct, but for what reason is it cut off?
2
QString PathToDb = QDir::currentPath() + "/MattyNotes.sqlite"; This option assigns the value "C: /Users/Matty/MattyNotes.sqlite" In general, I wonder where the intermediate folders are.
3
QString PathToDb = QFileInfo(".").absolutePath() + "/MattyNotes.sqlite"; This option gives "C: /Users/MattyNotes.sqlite"
What's happening? How easy is it to specify a relative path, so that everything eventually works on different machines?
And another question: why
if(QFile::exists(path)) // true if (!MattyNotesDb.open()) // open=true, то есть if(false) these lines were true, if in fact the opening of the database did not occur?
QFileInfo(path).absoluteFilePath(). - Cerbo