PDA

View Full Version : QProcess - Session and Permission



mariopettinati
5th December 2007, 19:45
Hi there. Yhis is my first post and I hope someone can help me.

I'm having a problem with QProcess. I'm trying to execute pg_dump (PostgreSQL backup generator) from my application.

I added the agument list containing information about user, host, etc. and setted the enviroment variables to store the password.

I captured the output and received a message from pg_dump that says:


pg_dump: [archiver (db)] connection to database "sgtnewnew" failed: could not translate host name "localhost" to address: Unknown host
pg_dump: *** aborted because of error

The command line I used was:


C:\Arquivos de programas\PostgreSQL\8.2\bin>pg_dump.exe -i -h localhost -p 5432 -U postgres -F c -b -v -f C:\testeBat1.backup sgtnewnew

I made many tests and it seams that pg_dump.exe has not found it dependencies or something else. I tried to create a .bat file and place it in the postgres/bin folder and then call it using QProcess. I got the same error message. When I ran this .bat file by clicking twice over it, the backup was sucessfully done.

I tried to run it using system(c++ function) and it worked fine. The problem is that I need to capture the output (stdout) and QProcess does it for me.

Dows anyone had any problem like this??

Thans for now

Mário

marcel
5th December 2007, 19:50
How do you start the process?
On win the arguments have to be contained within quotes. See the documentation for QProcess::start()

mariopettinati
5th December 2007, 19:57
This is my entire code. I seams pg_dump received all my parameters, but something was missed and it returned error. Note that I am using the full path of to pg_dump. Yhis _database objectc gives the all information about the database I want to backup :



string host = _database->host().empty() ? "localhost" : _database->host();
int port = _database->portNumber() == 0 ? 5432 : _database->portNumber();
string path = "C:\\Arquivos de programas\\PostgreSQL\\8.2\\bin";
string backupExe = "\"" + path + "\\pg_dump.exe\"";
string fileName = string(fileLineEdit->text().latin1());
string envPassword = "PGPASSWORD=" + _database->password();

if(!_process)
{
_process = new QProcess(0);
connect(_process, SIGNAL(readyReadStdout()), this, SLOT(readFromStdout()));
connect(_process, SIGNAL(readyReadStderr()), this, SLOT(readFromStderr()));
}
QDir dir(path.c_str());
_process->setWorkingDirectory(dir);

_process->clearArguments();
_process->addArgument(backupExe.c_str());
_process->addArgument("-i");
_process->addArgument("-h");
_process->addArgument(host.c_str());
_process->addArgument("-p");
_process->addArgument(Te2String(port).c_str());
_process->addArgument("-U");
_process->addArgument(_database->user().c_str());
_process->addArgument("-Fc");
_process->addArgument("-b");
_process->addArgument("-v");
_process->addArgument("-f");
_process->addArgument(fileName.c_str());
_process->addArgument(_database->databaseName().c_str());

QStringList* strList = new QStringList();
strList->append(envPassword.c_str());

if(!_process->start(strList))
{
QMessageBox::information(this, "Information", "Error starting process");
}

Thanks

--------------------------------------------------------------------------------

Obs: I am using QT 3.2.0

diogolr
8th July 2008, 08:07
Hello,

Using Qt 4.3... In Windows :P~...


Copy pg_dump.exe to your release folder
Copy all dependencies (zlib1.dll, libintl3.dll... ) to the same folder :p


This code work's great for me:


void ... :: on_backup_do_BD_triggered()
{
QString nome_arquivo = QFileDialog::getSaveFileName( this,
"Realizar Backup do Banco em...",
".",
"Arquivo SQL (*.sql)" );

QProcess pg_dump;

QString nome;
QStringList parametros;

// Definindo a variavel de ambiente que tera o password
QStringList var_de_ambiente;

var_de_ambiente << "PGPASSWORD=" + banco->password();

pg_dump.setEnvironment( var_de_ambiente );

#ifdef Q_OS_WIN32
nome = "pg_dump.exe";

int porta = banco->port();

parametros << "-f" << nome_arquivo
<< "-F" << "p"
<< "-U" << banco->userName()
<< "-p" << QString::number( porta == -1 ? 5432 : porta )
<< "-h" << banco->hostName()
<< banco->databaseName();
#endif

pg_dump.start( nome, parametros );
pg_dump.waitForStarted();
pg_dump.waitForFinished();

if ( pg_dump.exitCode() )
{
QMessageBox erro( this );

erro.setWindowTitle( "Erro de backup" );
erro.setIconPixmap( QPixmap( ":/48x48/alert.png" ) );
erro.setText( "Ocorreu um erro ao tentar fazero backup do BD." );

erro.exec();
}
}

NOTE: PGPASSWORD sets the password used if the server demands password authentication. Use of this environment variable is not recommended for security reasons (some operating systems allow non-root users to see process environment variables via ps);

aekilic
11th May 2009, 12:32
Dear All

I use the same way to backup, but the problem I have right not is to gzip the file,

I am using Windows and 4.5.0 I would like to gzip the backup I took.

aekilic
11th May 2009, 16:26
any body can help me for this?