How to Connect to SQL from R Studio - sql

I use Microsoft SQL Server Management Studio on Windows 10 to connect to the following database and this is what the login screen looks like:
Server Type: Database Engine
Server Name: sqlmiprod.b298745190e.database.windows.net
Authentication: SQL Server Authentication
Login: my_user_id
Password: my_password
This recent R Studio article offers an easy way to connect to SQL Servers from R Studio using the following:
con <- DBI::dbConnect(odbc::odbc(),
Driver = "[your driver's name]",
Server = "[your server's path]",
Database = "[your database's name]",
UID = rstudioapi::askForPassword("Database user"),
PWD = rstudioapi::askForPassword("Database password"),
Port = 1433)
I have two questions
What should I use as "[your driver's name]"?
What should I use as "[your database's name]"?
The server path I'll use is sqlmiprod.b298745190e.database.windows.net (from above) and I'll leave the port at 1433. If that's wrong please let me know.

Driver
From #Zaynul's comment and my own experience, the driver field is a text string with the name of the ODBC driver. This answer contains more details on this.
You probably want someting like:
Driver = 'ODBC Driver 17 for SQL Server' (from #Zaynul's comment)
Driver = 'ODBC Driver 11 for SQL Server' (from my own context)
Database
The default database you want to connect to. Roughly equivalent to starting an SQL script with
USE my_database
GO
If all your work will be within a single database then puts its name here.
In some contexts you should be able to leave this blank, but you then have to use the in_schema command to add the database name every time you connect to a table.
If you are working across multiple databases, I recommend putting the name of one database in, and then using the in_schema command to specify the database at every point of connection.
Example using the in_schema command (more details):
df = tbl(con, from = in_schema('database.schema', 'table'))
Though I have not tried it, if you do not have a schema then
df = tbl(con, from = in_schema('database', 'table'))
Should also work (I've been using this hack without issue for a while).

Related

How to read/fetch data from Oracle SQL database using PowerShell Core?

I have been researching on this for a couple of days but have been going in circles here.
I need to write a script that fetches the data from Oracle db and do something with the data. In my script I will have to fetch data multiple times.
My machine has the SQLDeveloper-21.4.3 which I got from installing InstantClient-Basic-Windows-21.3.0. I use the SQL Developer to connect to the db which is on another machine; this is how I can look into tables, views etc. of the db.
Secondly, this script will be hosted on another server that runs Windows-Server-2012-R2. I am just using my machine to write the script because I cannot use the server to do this. Therefore, I am looking for a solution that requires minimum amount of installing.
Thirdly, we do not have Oracle commercial license. This Oracle db I am trying to access is on the machine installed by a third party that installed some instruments. This company uses Oracle as they collect data on the instruments installed.
I was hoping the solution would be something similar to invoking connection to MS SQL where I downloaded module that gave cmdlets to connect to the MS SQL.
Oracle does have Oracle Modules for PowerShell but neither have I found information on how to use them nor have I understood the little information provided by Oracle on this. For this to work one of the requirement is:
A configuration file and key pair used for signing API requests, with
the public key uploaded to Oracle Cloud using Oracle Cloud
Infrastructure Console. Only the user calling the API should possess
the private key.
I don't know the heck Oracle is talking about here. Like, what is this configuration file, where is it? Where would I get the key pair from for signing API request. What is Oracle Infrastructure Console, where do I get it from? You get the idea.
Link: https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/powershell.htm
Therefore, I went the .DLL route.
This is what I have done so far:
I installed Oracle.ManagedDataAccess.Core -Version 3.21.61 from NuGet.
Unzipped the package and moved the Oracle.ManagedDataAccess.dll to the location of my script.
The code is:
$OracleDLLPath = "C:\Users\Desktop\CNC_File_Transfer_VSCode\Fastems_NicNet\Oracle.ManagedDataAccess.dll"
$datasource = " (DESCRIPTION =
(ADDRESS =
(PROTOCOL = TCP)
(HOST = 10.50.61.9)(PORT = 1521))
(CONNECT_DATA = (SERVER = DEDICATED)
(SERVICE_NAME = Fa1)
(FAILOVER_MODE = (TYPE = SELECT)
(METHOD = BASIC)
(RETRIES = 180)
(DELAY = 5))))"
$username = "username"
$password = "password"
$queryStatment = "SELECT [PROG_TYPE] FROM NC_PROGRAMS FETCH FIRST 10 ROWS ONLY"
#Load Required Types and modules
Add-Type -Path $OracleDLLPath
Import-Module SqlServer
Write-Host $queryStatment
#Create the connection string
$connectionstring = 'User Id=' + $username + ';Password=' + $password + ';Data Source=' + $datasource
#Creates a data adapter for the command
$da = New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($cmd);
#The Data adapter will fill this DataTable
$resultSet = New-Object System.Data.DataTable
#Only here the query is sent and executed in Oracle
[void]$da.fill($resultSet)
#Close the connection
$con.Close()
WRITE-HOST $resultSet
This gives an error though:
Add-Type : Unable to load one or more of the requested types. Retrieve
the LoaderExceptions property for more information.
I am new to programming in general. I would really appreciate if someone could provide detailed steps on resolving this. Thanks in advance.

"Data source name too long" error with mssql+pyodbc in SQLAlchemy

I am trying to upload a dataframe to a database on Azure SQL Server Database using SQLAlchemy and pyobdc. I have established connection but when uploading I get an error that says
(pyodbc.Error) ('IM010', '[IM010] [Microsoft][ODBC Driver Manager] Data source name too long (0) (SQLDriverConnect)')
I'm not sure where this error is coming from since I've used sqlalchemy before without a problem. I've attached my code below, can anybody help me diagnose the problem?
username = 'bcadmin'
password = 'N#ncyR2D2'
endpoint = 'bio-powerbi-bigdata.database.windows.net'
engine = sqlalchemy.create_engine(f'mssql+pyodbc://{username}:{password}#{endpoint}')
df.to_sql("result_management_report",engine,if_exists='append',index=False)
I know of other ETL methods like Data Factory and SSMS but I'd prefer to use pandas as the ETL process.
Please help me with this error.
Three issues here:
If a username or password might contain an # character then it needs to be escaped in the connection URL.
For the mssql+pyodbc dialect, the database name must be included in the URL in order for SQLAlchemy to recognize a "hostname" connection (as opposed to a "DSN" connection).
Also for mssql+pyodbc hostname connections, the ODBC driver name must be supplied using the driver attribute.
The easiest way to build a proper connection URL is to use the URL.create() method:
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
my_uid = "bcadmin"
my_pwd = "N#ncyR2D2"
my_host = "bio-powerbi-bigdata.database.windows.net"
my_db = "master"
my_odbc_driver = "ODBC Driver 17 for SQL Server"
connection_url = URL.create(
"mssql+pyodbc",
username=my_uid,
password=my_pwd,
host=my_host,
database=my_db, # required; not an empty string
query={"driver": my_odbc_driver},
)
print(connection_url)
"""console output:
mssql+pyodbc://bcadmin:N%40ncyR2D2#bio-powerbi-bigdata.database.windows.net/master?driver=ODBC+Driver+17+for+SQL+Server
"""
engine = create_engine(connection_url, fast_executemany=True)

oracle connection string not working

On a different oracle 11g server, this variant of connection string format works:
Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID))); User Id=myUsername;Password=myPassword;
but when I use this on another oracle 11g server with similar configuration, it doesn't work anymore.
When I use tnsping , the result comes out similar to the connection string above except the service name is blank.
Used EZCONNECT adapter to resolve the alias
Attempting to contact (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=))(ADDRESS=(PROTOCOL=TCP)(HOST=ip address)(PORT=port)))
OK (20 msec)
The DB is also reachable using the SQL Developer. What's wrong with my connection string? I'm working with a web service made in .NET that needs to connect to the oracle DB.
I think you missed out on this part SERVER = DEDICATED
datasource =(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = XXXX)(PORT = abc))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = my_orcl_db)

Not able to connect to OpenOffice Base - User lacks privilege or object not found Exception

I am trying to connect to an OpenOffice Base database from Java and execute a query, and have not been able to.
These are the steps I followed:
1) Created a Database 'TestDB.odb' in OpenOffice, and a table 'Movies' with columns (ID, Name, Director)
2) Downloaded hsqldb jar file and inclued in project build path
3) Used the following code to connect to it:
String file_name_prefix = "C:/Documents and Settings/327701/My Documents/TestDB.odb";
Connection con = null;
Class.forName("org.hsqldb.jdbcDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:" + file_name_prefix, "sa","");
Statement statement = con.createStatement();
String query1 = "SELECT * FROM \"Movies\"";
ResultSet rs = statement.executeQuery(query1);
Althoug I'm able to connect to the Database, it throws the following exception on trying to execute the query:
org.hsqldb.HsqlException: user lacks privilege or object not found: Movies
Tried googling, but have not been able to resolve my problem. I'm stuck and it would be great if someone could guide me on how to fix this issue?
You cannot connect to an .odb database. The database you have connected to is in fact a separeate set of files with names such as TestDB.odb.script, etc.
Check http://user.services.openoffice.org/en/forum/viewtopic.php?f=83&t=17567 on how to use an HSQLDB database externally from OOo in server mode. You can connect to such databases with the HSQLDB jar.
OLD thread.
I lost 2 days of my life until I changed the property:
spring.jpa.properties.hibernate.globally_quoted_identifiers = false
I was using mysql before and then I changed to hsqldb in order to run some tests. I kinda copied and pasted this property without looking and then you know - Murphy's law ...
I hope it helps.

command line to create data source with sql authentication?

I'm looking to run a batch file on windows xp professional which creates a system odbc data source for a sql server connection. I can do that with:
ODBCCONF.exe CONFIGSYSDSN "SQL Server" "DSN=Kappa| Description=Kappa Data Source | SERVER=10.100.1.10 | Trusted_Connection=Yes | Database=subscribers"
However I need a way to set the sql server authentication to be "With SQL Server authentication using a login ID and password entered by the user." to be set and preset the login id and pass.
Any ideas on how to do this?
thanks in advance
I hope this helps someone, i found that i could not store the username and password. However, I was able to make a ODBC connection without those. The problem was that the connection kept returning an error about how it could not locate the data source name or driver. Only after I removed the spaces from the attribute string could my programs use the odbc connection.
So
ODBCCONF.exe /a { CONFIGSYSDSN "SQL Server" "DSN=dsnname | SERVER=yourservername | Database=Finance"}
should become
ODBCCONF.exe /a { CONFIGSYSDSN "SQL Server" "DSN=dsnname|SERVER=yourservername|Database=Finance"}
create a .reg file with the parameters and merge it in with regedit.exe
Tip: export an existing ODBC profile from the registry to help get the syntax and field names correct
ODBCConf ConfigSysDSN "SQL Server" "DSN=xxxxx|SERVER=yyyyy|Trusted_Connection=No"
seemed to work for me.
You create the DSN the same way for both trusted and non trusted. It is when you use the ODBC connection string that you specify that you want to use trusted connections.
DsnString = "ODBC;DSN=Kappa;Database=MyDBName;Integrated Security=SSPI;Trusted_Connection=True"