RODBC to SQL using credentials file (as opposed to clear text) - sql

So this works:
odbcConnect("TestDB", "admin", "password")
but I dont want the username and pwd in clear text. I figured simply write the credentials to a text file and import that file for use with:
odbcConnect(textFile)
Saving the credentials and calling readLines(textFile) produces a string with escape characters that throws the connections:
"odbcConnect(\"TestDB\", \"admin\", \"password\")"
I've tried legion of escape characters in the text file and also tried creating a string using paste but the result always comes back to a version of the line above. Anyone got a workaround or safer way to connect to SQL?

You can store the variables in a separate .r file.
username = yourusername
password = yourpassword
dsn = yourDB
In the script where you connect the ODBC you can then call the script that defines those variables.
source("variables.r")

Related

Run sql plus commands on Unix machine

I am working on a unix machine and the only way to execute oracle sql commands is through a unix script we grant access like this :
#! /user/bin/ksh
User = 'PATH' # I can't read the file in this path
sqlplus $user << word # I don't know what it is used for
And then I start writing sql commands then execute the script through cmd
My question is:
Do I have any way to login to sqlplus directly through the info above through cmd
I tried to use this command to log in directly to SQL*Plus:
sqlplus $user << word # I don't know what it is used for
But it prompted username: # which I don't know
User = 'PATH' # I can't read the file in this path
There is no file to read. You are assigning the string literal 'PATH' to the environment variable "User". You could just as well say "User = 'FUBAR'"
sqlplus $user << word # I don't know what it is used for
It is telling the OS to launch the executable 'sqlplus', pass it the value of the environment variable "$user", then redirect other input from the next lines of the script until it comes to the string literal 'word'. This is call 'input redirection, and the commands between this line and the line beginning with the word 'word' are sometimes referred to as "a 'here' document". Using the string literal 'word' to terminate it is wierd and misleading at best. Most people use some variation of 'EOF' for this purpose.
I don't know what it is expected to be used for either. In *nix, environment variables (as are file names) are case sensitive. So this variable , "user" is not the same variable, "User" as you set in the previous line.
And then i start writing sql commands then execute the script through
cmd
I'm not sure what you mean by this. Are you saying that at this point, your script has sql commands intended to be processed by sqlplus? As indicated by your use of input redirection?
But it prompted username : # which I don't know
Well, in spite of all the other issues, if you don't know the username and password, you will never be able to connect to the database.
If your unix box is setup correctly the variable PATH should include /usr/local/bin you can test by typing in the command echo $PATH.
if its setup, the source in the oracle file oraenv like so
. oraenv
Note there should be a space between the period a the word oraenv. By doing this it should append the directories $ORACLE_HOME:$ORACLE_HOME/bin to the PATH variable. Since sqlplus is in $ORACLE_HOME/bin it should now be found.
I wouldn't recommend deviating from this standard. You should speak to your unix admin and Oracle dba to make sure this is setup correctly

Set Kettle DB Connection password from Environment Variable

How do I set a database connection's password to use an environment variable. I cannot add these to the kettle.properties file (security policy). When I click control-space on OSX, nothing happens. If I try and paste the string ${PASSWORD} directly into the password field, it does not allow it, due to the presumably non-alphanumeric '$' char.
The other fields as shown below work fine.
The EDIT Connection dialog below shows the field in red, which is what I want to achieve logically.
Using PDI Community 8.2, on Mac OSX Mojave.
I found one solution. Instead of creating the DB connection in Table Input Step with Edit or New, I used the Wizard option, which allowed me to paste ${SOURCE_DB_PASSWORD} variable into the password field.
The characters were obfuscated, so it's impossible to tell if you have the correct value, but it worked.
CTRL+V doesn’t work, you need to right-click and choose paste.
You can encrypt the password and save it on kettle.properties, in spoon directory execute:
Encr.bat -kettle yourpassword
Paste the full result with "Encrypted" in the properties file, restart spoon and test the connection.
In OSX, I believe you need to use the shortcut shift + cmd + space to access the environment variables.
You might need to use the 'get variables'-step to define the variables from kettle.properties.
Just type ${Variable_Name} as you are defining for other environment variable. If open your .ktr file in Notepad++ you can see the value is your Variable_Name instead of Encrypted password. It worked for me :)
enter image description here

Concatenating value to a command line

I am making a program that uses the rasidal command to connect to a VPN server. Obviously this requires a login and this is stored in a notepad file.
Process.Start("rasdial.exe", """VPN Connection"" HardcodedUSERNAME HardCodedPassword")
This seems to work fine, however when I read this information from a notepad file and write it to the screen they are still exactly the same. Why is it that the login could be incorrect.
All I am doing is
Process.Start("rasdial.exe", """VPN Connection"" & Info")
Thanks
Your variable "info" is never used. Try it out an remove the info-variable or rename it.
Do you have to double-quote "VPN Connection"? So try this one:
Process.Start("rasdial.exe", """VPN Connection"" " & Info)
And can you please tell me what your info-variable contains.

PostgreSQL setting pg_hba.config file

I am pretty new to databases and also using the command prompt. I've been trying to create a database but first I believe I need to set the pg_hba.config file. When I open SQL shell it says"
Server [localhost]:
ENTER
Database[postgres]:
ENTER
Port [5432]:
ENTER
Username [postgres]:
ENTER
Password for user postgres:
And then it won't let me type anything there, so I'm not really sure what to do from here.
It looks like you're on Windows, and you're starting psql.
When you type the password it's receiving it, but it's just not showing the characters as a security measure. It's like replacing the characters with bullets.
Just type the password and press enter.

stata odbc sqlfile

I am trying to load data from database (either MS Access or SQL server) using odbc sqlfile it seems that the code is running with any error but I am not getting data. I am using the following code odbc sqlfile("sqlcode.sql"),dsn("mysqlodbcdata"). Note that sqlcode.sql contains just sql statement with SELECT. The thing is that the same sql code is giving data with odbc load,exec(sqlstmt) dsn("mysqlodbcdata"). Can anyone suggest how can I use odbc sqlfile to import data? This would be a great help for me.
Thanks
Joy
sqlfile doesn't load any data. It just executes (and displays the results when the loud option is specified), without loading any data into Stata. That's somewhat counter-intuitive, but true. The reasons are somewhat opaquely explained in the pdf/dead tree manual entry for the odbc command.
Here's a more helpful answer. Suppose you have your SQL file named sqlcode.sql. You can open it in Stata (as long as it's not too long, where too long depends on your flavor of Stata). Basically, -file read- reads the SQL code line by line, storing the results in a local macro named exec. Then you pass that macro as an argument to the -odbc load- command:
Updated Code To Deal With Some Double Quotes Issues
Cut & paste the following code into a file called loadsql.ado, which you should put in directory where Stata can see it (like ~/ado/personal). You can find such directories with the -adopath- command.
program define loadsql
*! Load the output of an SQL file into Stata, version 1.3 (dvmaster#gmail.com)
version 14.1
syntax using/, DSN(string) [User(string) Password(string) CLEAR NOQuote LOWercase SQLshow ALLSTRing DATESTRing]
#delimit;
tempname mysqlfile exec line;
file open `mysqlfile' using `"`using'"', read text;
file read `mysqlfile' `line';
while r(eof)==0 {;
local `exec' `"``exec'' ``line''"';
file read `mysqlfile' `line';
};
file close `mysqlfile';
odbc load, exec(`"``exec''"') dsn(`"`dsn'"') user(`"`user'"') password(`"`password'"') `clear' `noquote' `lowercase' `sqlshow' `allstring' `datestring';
end;
/* All done! */
The syntax in Stata is
loadsql using "./sqlfile.sql", dsn("mysqlodbcdata")
You can also add all the other odbc load options, such as clear, as well. Obviously, you will need to change the file path and the odbc parameters to reflect your setup. This code should do the same thing as -odbc sqlfile("sqlfile.sql"), dsn("mysqlodbcdata")- plus actually load the data.
I also added the functionality to specify your DB credentials like this:
loadsql using "./sqlfile.sql", dsn("mysqlodbcdata") user("user_name") password("not12345")
For "--XYZ" style comments, do something like this (assuming you don't have "--" in your SQL code):
if strpos(`"``line''"', "--") > 0 {;
local `line' = substr(`"``line''"', 1, strpos(`"``line''"', "--")-1);
};
I had to post this as an answer otherwise the formatting would've been all messed up, but it's obviously referring to Dimitriy's code.
(You could also define a local macro holding the position of the "--" string to make your code a little cleaner.)