2
votes

I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-

echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db

basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.

But, when I call the script from my QT program

void MainWindow::on_importButton_clicked()
{    
    QProcess process;
    process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}

it compiles successfully but gives an error message in console when the button is pressed at runtime.

Error: near line 1: near "-": syntax error Error: cannot open "ora_exported.csv"

what could be causing this ???

EDITED

I have changed my .sh script now to--

echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db

Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.

Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.

2
script is run from directory where this file ora_exported.csv is not present and/or path variable doesn't contain directory where this file is located. Just add third argument: workingDirectory, with proper value. - Marek R
i have noted your suggestion and included directory path to the .csv in the script file. Still the situation is same. Can you guide me as to what the workingDirectory will be in my context. I was not able to get it from the QT docs link. - RicoRicochet
@MarekR was talking about the directory in which QProcess is called. Just add that directory to the startDetached() call OR call setWorkingDirectory() on the process instance, before calling startDetacjed(). - BaCaRoZzo

2 Answers

3
votes
  1. It looks strange that you are using external script to update database!
  2. why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
  3. I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{    
    QProcess::startDetached("/bin/sh",
                            QStringList()<<"/home/aj/script.sh",
                            "<location of: 'ora_exported.csv' file>");
}
4
votes

echo is a built in command of a shell that may behave differently.

E.g. take this test script: echotest.sh

echo -e "123"

Now we can compare different results:

$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123

You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.


Additionally, you are messing up your quotations

echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'

results in (no quotations in the first line)

attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported

but you pobably want

echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"