I'm able to establish the connection fine however I have been unable to figure out how to close the connection once I'm done. Any suggestion?
if (!require('odbc')) install.packages('odbc')
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "localhost",
Database = "VCF",
UID = "**********",
PWD = "**********"
)
dbWriteTable(con, "raw_myserversDF", myserversDF, append = TRUE)
I think this would help you by using RODBC. I also faced the same issue previously
con <- RODBC::odbcDriverConnect(...)
on.exit(RODBC::odbcClose(con))
From r2evans "DBI::dbDisconnect(con). In ?dbConnect, it says: "See Also: 'dbDisconnect()' to disconnect from a database"."
Related
I'm working with several R files that need connect to database but server has timeout so timeout=inf is not functional
What i was looking for is something like this:
RMySQL Database connection
(I'm the only user in R, and in total users just a few)
Inserting in my .RProfile all what is needed (credentials) and only in the .r programs that connect and disconnect when were necessary
In .RProfile:
con <- dbConnect(odbc::odbc(), Driver = "{MariaDB ODBC 3.1 Driver}",
Server = "{host}", database = "db", UID = "userid",
PWD = "pwd",
Port = 1234)
and in .R programs use something like this:
conn <- dbConnect(odbc::odbc(), group = "what i should use here?")
#using database
tbl(conn,"table")
#more code
dbDisconnect(conn)
I was too looking for other option, pool
In .RProfile
library(pool)
pool<- dbPool(odbc::odbc(), Driver = "{MariaDB ODBC 3.1 Driver}",
Server = "{host}", database = "db", UID = "userid",
PWD = "pwd",
Port = 1234)
.Last <- function(){
poolClose(pool)
}
But i'm not sure if it works or the previous option is better.
I'm having difficulty making a connection to my MS SQL Server from R.
I believe I'm using the right driver, I checked what drivers I had listed and picked the same one that I'm using in DBeaver for the connection to the same database:
odbcListDrivers() %>% filter(name %like% "SQL Server" & !name %like% "Teradata")
My connection string looks as follow:
db_conn <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "Server_address",
Database = "Database_Name",
UID = "myName",
PWD = "myPWd",
Trusted_Connection = "True",
Port = 1433,
ApplicationIntent = "ReadOnly"
)
I get the following error:
Error: nanodbc/nanodbc.cpp:1021: 01S00: [Microsoft][ODBC SQL Server Driver][SQL Server]The target database ('Database_Name') is in an availability group and is currently accessible for connections when the application intent is set to read only. For more information about application intent, see SQL Server Books Online. [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute
I can connect to the same database using DBeaver and have specified the the connection has applicationIntent = readonly
Is there some other connection string property I need to include to make a connection?
Turns out that I was using the correct connection string properties but the server was running in cluster mode and therefore I needed to append 'CLS' to my database name.
My final connection string looks as follows:
db_conn <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "SomeDBcls.xx.yy.zz.com",
Database = "NameOfSchema",
UID = "TheGoat",
PWD = rstudioapi::askForPassword("Database password"),
Trusted_Connection = "Yes",
Port = 1433,
applicationIntent = "readonly"
)
I have a connection to a mysql server like this:
mydb <- DBI::dbConnect(
drv = MySQL(),
host = "host",
port = 1111,
user = "user_1",
password = "password",
dbname = "database_name"
)
and then I do queries to that data table using this code
query1 <- fetch(dbSendQuery(mydb, "select * from table_1"), n = Inf)
so the result is that I have a query1 table in the R environment.
Now I have other databases in SQL Server, so I'm trying to do the same. I'm establishing the connection doing this:
con <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "server",
Database = "database_2",
UID = "user_2",
PWD = "password",
Port = 2222)
and it seems that works, because in the Connection tab appears the database, but when I navigate and try to see the data an error occurs. Besides this, I'm looking for functions that do the same like the previous (fetch with dbSendQuery), having this way the data frames available in the environment.
I finally solved the problem using this code:
con <- dbConnect(odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Server = "server",
Database = "database_2",
UID = "user_2",
PWD = "password",
Port = 2222)
So the problem was in the Driver.
I want to connect RStudio to a database in PostgreSQL. So firstly following the instructions I modified the odbcinst.ini file:
[PostgreSQL Driver]
Driver = /usr/local/lib/psqlodbcw.so
Ok, so now appears a PostgreSQL selectable in connection. And then I tried to make the connection:
con <- dbConnect(odbc::odbc(),
.connection_string = "Driver={PostgreSQL Driver};",
dbname = 'name',
host = 'host',
user = 'user',
password = 'pass',
port = 5432,
timeout = 10)
But it returns me an error:
rror: nanodbc/nanodbc.cpp:983: 00000: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I have tried so far:
drv <- dbDriver("RPostgreSQL")
con <- dbConnect(drv,
dbname = 'name',
host = 'host',
user = 'user',
password = 'pass',
port = 5432)
That works but I'm unable to navigate the database (I mean, to see the tables and connection status) in RStudio.
My question is: Should I change my driven in odbcinst.ini? What would be the .so file? Any help pointing what to do will be greatly appreciated. By the way I'm running on Mac, and the host is in Amazon.
Perhaps RPostgres can help you out
con <- RPostgres::dbConnect(RPostgres::Postgres(),
dbname = 'name',
host = 'host',
user = 'user',
password = 'pass',
port = 5432)
After some trial and error this worked for me:
con <- dbConnect(odbc::odbc(),
Driver = "PostgreSQL Driver",
Database = 'name',
Server = 'host',
UID = 'user',
PWD = 'pass',
Port = 5432,
timeout = 10)
Voila, now I can see the tables in the database.
I am trying to connect to my MS SQL database with RJDBC and I don't know what to fill in url argument. With odbc this was enough:
dbConnect(odbc::odbc(),
Driver = "SQL Server",
dsn = "MyDsn",
uid = "User",
pwd = "123456",
server = "myserver123456\\myserver1",
database = "MyDatabase")
When I swap the driver from odbc to jdbc then it fails:
dbConnect(RJDBC::JDBC(classPath = "C:/jdbc/mssql-jdbc-7.0.0.jre8.jar"),
Driver = "SQL Server",
dsn = "MyDsn",
uid = "User",
pwd = "123456",
server = "myserver123456\\myserver1",
database = "MyDatabase")
error: Error in .jcall("java/sql/DriverManager", "Ljava/sql/Connection;", "getConnection", :
argument "url" is missing, with no default
What should i write in url argument? How to get know?
RJDBC uses different arguments for the dbConnect function: a driver definition and a connection URL (the piece you are missing). For example (from https://www.rforge.net/RJDBC/), to connect to a MySQL Database, your code would look like the following:
library(RJDBC)
drv <- JDBC("com.mysql.jdbc.Driver",
"/etc/jdbc/mysql-connector-java-3.1.14-bin.jar",
identifier.quote="`")
conn <- dbConnect(drv, "jdbc:mysql://localhost/test", "user", "pwd")
Loads a JDBC driver for MySQL (adjust the path to the driver's JAR file as necessary) and connects to the local database "test". The connection handle conn is used for all subsequent operations.
For SQL Server, your code will look something like the following (from https://www.r-bloggers.com/connecting-to-sql-server-from-r-using-rjdbc/):
require(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"C:/jdbc/mssql-jdbc-7.0.0.jre8.jar")
conn <- dbConnect(drv, "jdbc:sqlserver://serverName", "userID", "password")