I want to make a small program that automatically installs an extension to PostgreSQL, and works on both Windows and Linux.
To do this, the strategy that has occurred to me (and because the default PostgreSQL installation directory can be any directory, and more so on Linux), is to search for the extension plpgsql
, which is always installed. This way I can find the path where the extensions are hosted.
For this I start from this:
#include <QCoreApplication>
#include <QDirIterator>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDirIterator it("/usr", QDirIterator::Subdirectories);
//QRegExp rx("sql$");
while (it.hasNext())
{
if (it.next().contains("plpgsql--1.0.sql"))
{
qDebug() << it.next();
}
}
qDebug()<<"Fin";
return a.exec();
}
I am making use of the method:
bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Well, the result it gives me is:
"/usr/share/postgresql/10/extension/btree_gin--1.0.sql"
Fin
When the searched file actually exists. plpgsql--1.0.sql
in that same directory.
Does anyone know how to interpret this result?
Method documentation
next
:Which means something like each call to
next
advances the iterator .Well, notice that you are calling twice to
next
:That is to say,
contains
it works correctly, what happens is that the output ofqDebug
is showing you the following file.What you have to do is store the file name in a variable to avoid recurring calls to
next()