Concatenating Substition variables with strings - sql

I am modyfying a SQLplus script that creates a user, connects to the user and creates tables and puts values in those tables. This script had a set username and password for user like the below
CONNECT store/store_password#test
However I want to replace store_password with a variable v_store_password which I have put at the start:
ACCEPT v_store_password CHAR PROMPT 'Please type in store password: ' HIDE
Basically what I tried was this:
CONNECT store/&v_store_password || #test;
The above unfortunately does not work. I read some where that a "." can be used but it doesn't work.
How to concatenate or add to Substition variables?
Also how to add numbers to these variables?

/*make a file called file.connection.txt and enter username#password#tnsname#*//* write below code into batch file*/CLSECHO TITLE Database test script (ORACLE)#echo offclsfor /f "tokens=1-5 delims=#" %%a in (file_connection.txt) do (sqlplus -l "%%a/%%b#%%c " #test.sql)
exit

I actually dicovered that I have not included the & in front of variable like below
CREATE USER store IDENTIFIED BY v_store_password;
and the "." is varables concatenation character which ends them so the solution is:
CONNECT store/&v_store_password.#test;

Related

How can I insert the content of the variable into single quotes inside the INSERT INTO command?

I created a text file. The name of this is "test.txt" and the content is first part below. I also created script with the name insert.sh.
I run the command with ./insert.sh test.txt.
If the words / strings are in single quotes, it will insert the words into the columns. Also it will insert numbers without single quotes. The csv that I will eventually use won't have single quotes and I don't want to change the data.
How can I insert the content of the variable into single quotes inside the INSERT INTO command?
I am using psql.
Text file, test.txt
'one','ten','hundred'
'two','twenty','twohundred'
Script, insert.sh:
#!/bin/bash
while read cell
do
name=$cell
echo "$cell"
####Insert from txt into table####
sudo -u username -H -- psql -d insert_test -c "
INSERT INTO first (ten, hundred, thousend) VALUES ($cell);
"
done < $1
something like this:
INSERT INTO first (ten, hundred, thousend) VALUES (INSERT" $cell "QUOTES);
UPDATE:
I changed the code. I added the single quotes around $cell as you suggested.
#!/bin/bash
while read cell
do
name=$cell
echo "$cell"
####Insert from txt into table####
sudo -u username -H -- psql -d insert_test -c "
INSERT INTO first (ten, hundred, thousend) VALUES ('$cell');
"
done < $1
and I removed the quotes out of the text file since the csv file that I want to use later wont have any single quotes.
new text file.
one,ten,hundred
two,twenty,twohundred
and im getting the error:
one,two,three
ERROR: INSERT has more target columns than expressions
LINE 2: INSERT INTO first (ten, hundred, thousend) VALUES ('one,two,...
You need to modify the $IFS (Internal Field Separator) variable to determine the line separator used by Bash. Since you used a CSV like file, you IFS come to , character, thus this is the result $IFS=,. Note that if you need to do others stuff in you script, you need to redefine the $IFS var to the original state, so you need to store it in an temportal variable before you change it, something like $OLDIFS=$IFS.
readline read the entire line and separate the values depending on $IFS var, thus you need to write the adecauted quantity of var where readline will store the words, i.e., if you line have 3 words, you need to give 3 vars to readline, e.g.: file: foo,baz,bar, readline -r word1 word2 word3. If you don't give the correct amount of vars, readline will store the rest of word in a single var, that is your problem.
So, a solution to your problem would be:
#!/bin/bash
$OLDIFS=$IFS # If you need to do more stuff.
while IFS=, read -r word1 word2 word3
do
sudo -u username -H -- psql -d insert_test -c
"INSERT INTO first (ten, hundred, thousend) VALUES (${word1}, ${word2}, ${word3});"
done < $1
$IFS=$OLDIFS # Same of line 2.
# ...
NOTE: This is insecure because lead with easily to a SQL injection. If you use this, only use in a local database that don't have any sensetive data.

three dots menu of some sort windows sqlite3

When I try ls -1 (in powershell) to try and get into my table and check it, I get some sort of menu "...>" that I can't .quit out of. when I close out and run sqlite3 -init ex1.sql ex1.db (my example I'm working with, I get " Error: near line 1: table person already exists " when it in fact, does not. how do I get out of this menu, and how do I fix my code? Also, what IS this menu?
My sql code:
CREATE TABLE person (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
age INTEGER
);
...> is shown by the sqlite3 command-line shell (not PowerShell) when you have not finished the current SQL command. Typically, you forgot the terminating ;, or you forgot a ' and are still inside a string.
Maybe you didn't specify what is the database in which you are working. For example before creating the table, use .open <name_database.db> and after run you CREATE TABLE statements.
Also the 3 dots ...> means that you didn't write the complete command, most often you didn't put the semicolon ; or you didn't close well the '. You can exit from this menu by killing the process or complete the command.

PLSQL SQL script combining parameters with connect

Does anyone know if what I'm trying to do in the below code is possible and if so what the syntax is? This issue is around the connect call, the username doesn't seem to generate correctly. The commented out connect call is another one I tried.
-- myscript.sql
-- #params:
-- 1 - Oracle database name eg. localhost
-- 2 - Site (site01, site02 site03)
connect systemname_%2_admin/mypassword#&1;
--connect "systemname_" || "%2" || "_admin"/mypassword#&1;
begin
--execution code here.
end;
/
disconnect;
NOTE: Call does need to be this way as this is going to be an automated script doing different things for different usernames.
Your arguments will be stored in substitution variables 1, 2 and so on.
You access them in your script with &1, &2 (so forget about %2, it's meaningless).
Now your problem is that &2_admin looks to sqlplus like a substitution variable named 2_admin so you just need to add a dot . after the 2. Dot is the character that separates the name of a substitution variable from what follows.
your connect will look like :
connect systemname_&2._admin/mypassword#&1
(With no ; : this is a sqlplus command not an sql statement).

MS DOS IF and % percent

I haven't done DOS in forever and can't recall the idiosyncracies with prefixing labels with %. I'm trying to write a script (others may also find it useful) to connect to ADB to a WiFi service running on my tablet.
#echo off
set def=192.168.1.21
if "%1" == "" (
echo.Please supply the IP address of the ADB server/tablet.
set /p ip=[%def%]
if %ip% == "" (
set ip=%def%
)
) else (
set ip=%1
"C:\Program Files\adt-bundle-windows-x86_64-20130219\sdk\platform-tools\adb" connect "%ip":5555
)
The script should accept an IP address on the commandline, but if none is supplied, if should prompt the operator. If nothing is entered, the printed, square bracketed default should be used.
It seems to work, except that
set ip=%def%
never executes. I think I messed up the second IF statement. I just can't get the % signs in the right place!! Does anyone remember this old black magic?
Thanks.
No need to set ip to the default if the user didn't enter anything. SET /P will preserve the existing value if the user presses Enter without typing anything. So all you need to do is set the value to the default before issuing SET /P.
Problems can occur if the user provides quotes for the arguments, and then your code adds additional quotes. It is generally safer to use "%~1" - the tilde strips any existing quotes, and then you add your own. This works if the argument is already quoted or not.
You probably should remove any existing quotes when you SET ip in your ELSE statement.
You are missing a % when you attempt to expand ip as an argument to adb. I suspect that you always want to execute the command. Your current code only executes the command if ip is provided as an argument.
I would structure the code as follows:
#echo off
setlocal
set "def=192.168.1.21"
set "ip=%~1"
if not defined ip (
set "ip=%def%"
echo Please supply the IP address of the ADB server/tablet.
set /p "ip=[%def%] "
)
"C:\Program Files\adt-bundle-windows-x86_64-20130219\sdk\platform-tools\adb" connect "%ip%:5555"

Appending a message to a buffer from a Vim script

What's the best way to append the content of a string variable from a Vim script?
If variable contains no newlines, then use
call append(line('$'), [variable])
, you can also do
call append(line('$'), split(variable, "\n"))
,
execute "normal! Go".variable
, or
execute "normal! Go\<C-r>\<C-r>=variable\<CR>"
You could also put the variable into a register like this:
let #a = variable
normal! G
execute "put a"
This works with or without carriage returns.