Linux - Store a sql select value in a variable bash - linux

I want to store the value of the sqlite statement in a variable
backup=$(sqlite3 "/home/miguel/Desktop/SO/ProjetoFinal/Backup_Principal.db" "SELECT periocidade_backup FROM STORAGE WHERE path'$path';")
But when i echo $backup it returns the following:
sqlite3 "/home/miguel/Desktop/SO/ProjetoFinal/Backup_Principal.db" "SELECT periocidade_backup FROM STORAGE WHERE path='$path';"
What am I doing wrong?

the part of your code '$path' is using a single quote which is literal and show exactly as what is in the quotes, which would not use the variable's value. using speech marks like the following should work, "'$path'"

Related

Quotes missing in Hive Query - HQL

I am calling HQL from shell script.
I am passing variable to HQL from querying from other table. Variable I am passing as follows:
$$A1=('123','124')
I see variable $$A1 properly in shell script with echo statement and it displayed as ('123','124').
but when I am using this variable in query, its missing single quotes. I mean it is passing as (123,124)
I am passing as $$A1 as follows:
select * from table1 where cd in $$A1
query is taking as select * from table where cd in (123,124)
why single quotes are missing when it is passing to the query.
appreciate any help on this.
Thanks,
Babu

Mongo output to shell variable

Is it possible to store mongo output into a variable in shell script, below is the example query which prints decimal date
ex: 1489442900000
maxdate =$( echo mongo getmaxdate.js --quiet)
Any help is highly appreciated.
Thanks,
Ashwin.
There should be no space between the variable name and the command during the assignment.
variable_name=$(command1 |command2 .....)
In your case:
maxdate=$( mongo getmaxdate.js --quiet)
Make sure to preserve the formatting of the output. if echo'ed Without double quote the whole content will show up in one line.
echo "$maxdate"

BigQuery Command Line - How to use parameters in the query string?

I am writing a shell script which involves BigQuery commands to query an existing table and save the results to a destination table.
However, since my script will be run periodically, I have a parameter for the date for which the query should run.
For example, my script looks like this:
DATE_FORMATTED=$(date +%Y%m%d)
bq query --destination_table=Desttables.abc_$DATE_FORMATTED "select hits_eventInfo_eventLabel from TABLE_DATE_RANGE([mydata.table_],TIMESTAMP($DATE_FORMATTED),TIMESTAMP($DATE_FORMATTED)) where customDimensions_index = 4"
I get the following error:
Error in query string: Error processing job 'pro-cn:bqjob_r5437894379_1': FROM clause with table wildcards matches no table
How else can I pass the variable $DATE_FORMATTED to the TABLE_DATE_RANGE function from BigQuery in order to help execute my query?
Use double quotes "" + single quote ''. For example, in your case:
TIMESTAMP("'$DATE_FORMATTED'")
OR
select "'$variable'" as dummy from your_table
You are probably missing the single quotes around the $DATE_FORMATTED value inside the TIMESTAMP functions. Without the quotes it's going to be defaulting to the EPOCH time.
Try with:
TIMESTAMP('$DATE_FORMATTED'),TIMESTAMP('$DATE_FORMATTED')

What am I missing in trying to pass Variables in an SSIS Execute SQL Task?

I am creating an SSIS Execute SQL Task that will use variables but it is giving me an error when I try to use it. When I try to run the below, it gives me an error and when I try to build the query, it gives me an error SQL Sytnax Errors encountered and unable to parse query. I am using an OLEDB connection. Am I not able to use variables to specify the tables?
You can't parameterize a table name.
Use the Expressions editor in your Execute SQL Task to Select a SqlStatementSource property.
Try "SELECT * FROM " + #[User::TableName]
After clicking OK twice (to exit the Task editor), you should be able to reopen the editor and find your table name in the SQL statement.
Add a string cast in a case where it might be a simple Object - (DT_WSTR,100)
You are using only single parameter(?) in the query and assigning 3 inputs to that parameters which is not fair put only single input and assign some variable as input as shown in image and change the value of variable respectively.
the parameter name should be incremented by 1 start with 0 because they are the indexes representing the "?" in the query which was written the query window.

Save Result of Shell Script SQLite Select Statement to Variable

I'm pretty new to shell scripts and am only doing them because its about time I learnt and I need to for work.
I have been looking around and have tried multiple methods to get this working but can't seem to figure it out.
I have a script in which I want to access an SQLite database and store the result of a select statement in a variable.
What I've Tried So Far
This one just echoes whats inside the apostrophe. If I remove the dollar sign before the apostrophe I get the same outcome.
track_name=$'sqlite3 "$database_name" << EOF
select name from track where id = "$required_track";
exit;
EOF'
Here I get a syntax error near "track_name"
sqlite3 "$database_name" << EOF
track_name='select name from track where id = "$required_track";'
exit;
EOF
I have successfully executed the select statement without trying to store it in a variable but its not much use to me without being able to store it...
Any help would be much appreciated
To store the output of a command into a BASH variable you should use:
VAR_NAME=$(command);
For example, if you want to store your system current time into a variable or
the results of a list directory command ejecution:
DATE_EXAMPLE_VAR=$(date); #Stores 'date' command output into DATE_EXAMPLE_VAR
echo $DATE_EXAMPLE_VAR; #Shows DATE_EXAMPLE_VAR contents
DIRCONTENTS=$(ls); #Stores a list of your current directory contents.
Similarly, this should work for sqlite3:
track_name=$(sqlite3 "$database_name" "select name from track where id = $required_track")

Resources