Error while running a .sh script via QProcess - qt

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.

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"

It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
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>");
}

Related

How can I quit sqlite from a command batch file?

I am trying to create a sealed command for my build pipeline which inserts data and quits.
So far I have created my data files
things-to-import-001.sql and 002 etc, which contains all the INSERT statements I'd like to run, with a file per table.
I have created a command file to run them
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.quit
However when I run my command
sqlite3 -init ./import-all.sql ./database.sqlite
..the data is inserted, but the program remains running and shows the sqlite> prompt, despite the .quit command. I have also tried using .exit 0.
From the sqlite3 --help
-init FILENAME read/process named file
Docs: https://www.sqlite.org/cli.html#reading_sql_from_a_file
How can I tell sqlite to exit once my inserts have finished?
I have managed to find a dirty workaround for this issue.
I have updated my import file to include a bad command, and executed using -bail to quit on first error.
-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.fakeErrorToQuitWithBail
Then you can execute with
sqlite3 -init import-all.sql -bail
and it should quit with
Error: unknown command or invalid arguments: "fakeErrorToQuitWithBail". Enter ".help" for help
Try using ".exit" at the place of ".quit". For some reason SQLite dont doccumented this commands.
https://www.tutorialspoint.com/sqlite/sqlite_commands.htm

BTEQ: Save Teradata error in logfile

My goal is to automate a deployment of SQL scripts to Teradata via BTEQ. So far my script is working. However, I would like to generate a log file where possible failures are captured.
.LOGON tdserver/username,pw
.EXPORT file=\logfile.txt;
.run file = \Desktop\test\test.sql;
.LOGOFF
.EXIT
My SQL script will create a VIEW. When this view for example already exists I see an error in the BTEQ command window:
*** Failure 3804 View 'ViewName' already exists.
I would like to have this TD Message in my log file. I tried several tings, have been looking for 3 hours but unfortunately without success.
You may want to experiment using .SET ERROROUT STDERR which re-routes the error stream to the STDERR output file instead of the default action of routing the error stream to STDOUT.
There is more information in the BTEQ manual under Chapter 5 - BTEQ Commands.
Save all your script written above as a text file and create a batch file that generates a log after running the script:
echo off
bteq < script.txt > script.log 2>&1
#echo off goto end
:end
#echo exit
Errors will be recorded in this way.

unable to execute unix script using . script_name

I am unable to execute following command in unix as
e.g. ->
export ENVFILE=$PARENT_DIR/../env/.tpms.evr
while i try to execute . "${ENVFILE}" it shows me error as bash: 39910-: command not found
can anybody let me know about how to fix this.
You most probably have a line in .tpms.evr script which bash tries to execute as a command called "39910-"
Please share this .tpms.evr script or just that line containing "39910-"

Sqlite Mode Error When Running Script with Output Redirection

I have the following shell script
cat <<EOF | sqlite3 /path/to/my/db.sqlite
.mode line;
select item from mytable;
EOF
When running the script with output redirection, I get this error:
Error: mode should be one of: column csv html insert line list tabs tcl
This happens also when I try different modes. When I run the script without output redirection (printing to terminal), it works fine. What's going on?
Figured it out-- actually the same error was being thrown without output redirection, but I was missing it because normal output was being dumped to the screen instead of a file. The problem in my script is that there should be no ; after mode line.

Teradata - run a file/script at Unix Linux command prompt

I have the client/TTU installed on Unix box for Teradata.
If I do the following, it works. Where "..." is Teradata BTEQ normal output and once the following is done, I'm back at the prompt.
$ bteq
...
....
. logon dbname/dbuser,dbpassword
SELECT DATE, TIME;
.LOGOFF;
.QUIT;
..
...
$
Now, lets say I put the following lines in a file called "testtd.bteq"
. logon dbname/dbuser,$dbpassword
SELECT DATE, TIME;
.LOGOFF;
.QUIT;
What I want now is ... how can I run this script (.bteq) at Unix $ prompt ???
I tried the following methods so far, but they didn't work, may be Im missing anything:
1. bteq < /path/to/testtd.bteq > testtd.log
2. bteq <
.run /path/to/testtd.bteq
HereDocEndsHere
Any ideas? DO I HAVE to provide ". logon dbname/dbuser,dbpassword" FIRST, if I'm using the HereDocument way?
Running bteq command on $ prompt doesn't even give me any HELP/options that I can use, like other commands.
i.e.
cmd -u user -p password -f file etc...
The best practice I'm aware of is
store your teradata credentials in a ~/.tdlogon file
create a script that contains your bteq call with all the stuff it needs.
E.g., create a file bteqScript.sh with
/* define helper variables, e.g.... */
export ARCHIVEDIR=~/data
export DATAFILE=dataOutput1.txt
bteq <<EOF
.run file=$HOME/.tdlogon
.export data file=${ARCHIVEDIR}|${DATAFILE}
/* sql code on next line(s) */
select
'foo' as "bar"
;
.export reset
EOF
Note that .run file=... executes the .logon command with your credentials, stored elsewhere.
Kudos to Alex Hasha for the bteq script.
PS - It works via method 1 -- when I hard code the password in the script file for LOGON command.
I wanted to do the same via exporting a variable called "dbpassword"
i.e.
$ export dbpassword=xyxyxyxyx
and
then, inside the script file, i can use "$dbpassword" ... in the LOGON command.. somehow export is not exporting the var within .bteq logon command.

Resources