Resolving conflict with RSQLite and RMySQL in the same session - r

I don't know that this is an RSQLite/RMySQL conflict, but that's my best guess so far.
I use RMySQL to get data from a MySQL database, then I close that connection with dbDisconnect(). Then I use these data to in a record linking process using the RLBigDataLinkage() function in the RecordLinkage package. This function uses an SQLite database and the RSQLite package because there are two many possible matches to keep all of them in memory. As far as I can tell from the documentation, there is no way to explicitly close the SQLite connection.
Then I use these matches to pull a larger dataset from my MySQL database, again with RMySQL. I can connect to the database, but when I query, I get the following error:
Error in mysqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: MySQL server has gone away)
This happens even with trivial queries.
> dbGetQuery(db, "SELECT * FROM ann_id_info LIMIT 5")
Error in mysqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: MySQL server has gone away)
I can still query from the MySQL command line, though.
Is there a way I can disconnect an "unknown" RSQLite connection? Thanks!

Related

How to use R DBI to create a view?

I'm trying to use R's DBI library to create a view on an Athena database, connected via JDBC. The dbSentStatement command, which is supposed to submit and execute arbitrary SQL without returning a result, throws an error when no result set is returned:
DBI::dbSendStatement(athena_con, my_query)
Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set", :
Unable to retrieve JDBC result set
JDBC ERROR: [Simba][JDBC](11300) A ResultSet was expected but not generated from query <query repeated here>
In addition, the view is not created.
I've tried other DBI commands that seemed promising (dbExecute, dbGetQuery, dbSentQuery), but they all throw the same error. (Actually, I expect them all to - dbSendStatement is the one that, from the manual, should work.)
Is there some other way to create a view using DBI, dbplyr, etc.? Or am I doing this right and its a limitation of RJDBC or the driver?
RJDBC pre-dates the more recent DBI specification and uses a different function to access this functionality: RJDBC::dbSendUpdate(con, query) .
DBI's dbSendStatement() doesn't work here yet. For best compatibility, RJDBC could implement this method and forward it to its dbSendUpdate() .
Without given more details of your query, I cannot promise this helps:
But for my case:
nrow <- dbExecute(con, paste0("CREATE VIEW ExampleView AS",
"Random statements"))
Would help you create a view on your backend.
Difference: I'm using SQLite.

Connect to MySQL database with RMySQL unknown database

I am trying to connect to a MySQL database through RMySQL but get the following error
"Error in .local(drv, ...) :
Failed to connect to database: Error: Unknown database 'XXX'"
Has anyone had a similar issue and was able to resolve it?
Running
macOS High Sierra, Version 10.13.6
MySQL workbench 8.0
RStudio Version 1.1.453
I constructed the SQL driver as follows:
install.packages("RMySQL")
install.packages("dbConnect")
library(DBI)
library(dbConnect)
con <- dbConnect(RMySQL::MySQL(),
dbname = "xxx",
host = "xxx",
port = xxx,
user = "xxx",
password = "xxx")
I've been following Filip Schouwenaars' datacamp course Importing Data in R (https://www.datacamp.com/courses/importing-data-in-r-part-1) and was hoping to establish a connection to the SQL database and create an MySQLConnection object to then run SQL queries from inside R.
The problem is that I get stuck at the very beginning because of the failure to connect to database. In MySQLworkbench, the script opens and looks great. I'm a complete newbie at this, and am wondering whether this may have something to do with the location of the database file itself? Should I be saving it in a specific folder?
PS: I've read through all RMySQL threads on here and could not find a solution; if I missed something, please let me know. This is my first ask on this forum, and I'm both super grateful for the community here but also worried that I missed something, somewhere. THANK YOU for your help.
Solution: I did in fact not have a 'database' (or schema, as they are now called in MySQL), but merely an .sql file. Once I created a database from the file, it worked like a charm!
For other newbies out there, especially in the humanities, this was a very helpful tutorial on how to set up MySQL with R: https://programminghistorian.org/en/lessons/getting-started-with-mysql-using-r

R connection to SQL Server

I have been trying to make a connection between R and the SQL server but I keep getting this error msg:
object 'C_RODBCDriverConnect' not found
It seems as if R is trying to find that object but is failing in doing so, anyone have an idea on how I can solve this issue?
Reinstalling R is not an option as it's a work computer and I do not have the rights to do so. Also note that I am using the RODBC package as the odbc package doesn't want to install properly (I kept getting the zero non-exit error msg).
Thanks in advance.
Zachary
Just make sure you make a odbc connection in the odbc data sources and ensure you have done it correctly to the sql server database.
library(RODBC)
#ODBC_1 refer to Database you want to use
con_ref = odbcConnect("name_of_connection")# name of connection is what you used in odbc connection setup
#input data
x <- sqlQuery(con_ref, "select * from db_name")

How to check if the connection to MySql through RMySql persists or not?

I have connected to a mysql database through the RMySQL package using this statement:
con<-dbConnect(drv=RMySQL::MySQL(max.con=1,fetch.default.rec=500),host="host",dbname="dbname",password="psswd",user="user"))
So far no issues. When I check:
>class(con)
[1] "MySQLConnection"
attr(,"package")
[1] "RMySQL"
After an hour I used the following statement:
dbGetQuery(conn=con,"show tables")
and I got an error:
Error in .local(dbObj, ...) :
internal error in RS_DBI_getConnection: corrupt connection handle
However, If I check this statement:
dbListConnections(drv=RMySQL::MySQL())
It gives:
[[1]]
<MySQLConnection:0,21>
When I try:
dbDisconnect(conn=con)
I get the same error:
Error in .local(dbObj, ...) :
internal error in RS_DBI_getConnection: corrupt connection handle
Then I removed the connection object:
rm(con)
When I tried to connect again using dbConnect(), I got this error:
con<-dbConnect(drv=RMySQL::MySQL(max.con=1,fetch.default.rec=500),host="host",dbname="dbname",password="psswd",user="user"))
Error in .local(drv, ...): Cannot allocate a new connection: 1 connections already opened
I know that the dbListConnections() call returns an empty list when there is no connection to the database. But in this case does not return an empty list.
Is the corrupted connection handle a different state of the connection than the disconnected state?
OR
The connection has timed out?
What is the best approach to keep a check if the connection to DB is still working?
You still might need to close the connection with dbDisconnect(con) instead of rm(con) to free the internal connection handle and allow a new connection. The latter only removes a "pointer" to the connection object (so that you cannot access this object anymore via con), but it still exists physically until garbage collection.
You could check if the connection is valid via dbIsValid(con), or use a simple dbGetQuery(con, "SELECT 1"). I'd be interested to know if the former detects a disconnect on your system, there's a discussion on GitHub around this topic.

Querying a linked SQLite DB in SSMS

I'm trying to use a SQLite database a linked server in SSMS. I've managed to get the ODBC driver installed and a linked server created, but I can't seem to find a way to get queries to work. I think it's just a matter of not understanding the proper syntax for it. Here's what I've tried:
exec sp_tables_ex 'SQLITE'
This works as expected, showing all of the tables in the database.
select * from SQLITE.[default].dbo.TRANSLATION
Fails with this error message
Invalid use of schema or catalog for OLE DB provider "MSDASQL" for
linked server "SQLITE". A four-part name was supplied, but the
provider does not expose the necessary interfaces to use a catalog or
schema.
Taking a clue from that, I tried removing the schema:
select * from SQLITE.[default].TRANSLATION
But this gives me another error message:
Invalid object name 'SQLITE.default.TRANSLATION'.
Likewise, the following give the same error (with slight changes for the object name):
select * from SQLITE.[default].TRANSLATION
select * from SQLITE.dbo.TRANSLATION
select * from SQLITE.TRANSLATION
Any ideas? I'm not quite sure what to try from here.

Resources