Quantmod: Error loading symbols from MySQL DB - mysql

I am trying to fetch symbols from a MySQL db using getSymbols, however the following code
library(blotter)
library(DBI)
library(RMySQL)
setDefaults(getSymbols.MySQL,user="****", password="****", dbname="quantmoddb")
currency("USD")
stock("myspy",currency="USD",multiplier=1)
getSymbols("myspy",src="MySQL")
throws
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘dbConnect’ for signature ‘"character"’
I am pretty sure the db is set up correctly as the following code runs fine and returns the dataset as expected.
con <- dbConnect(RMySQL::MySQL(),user="****",password="****", dbname="quantmoddb",
host="localhost", port=3306)
db.Symbols <- DBI::dbListTables(con)
query <- paste("SELECT * from myspy ORDER BY date")
rs <- DBI::dbSendQuery(con, query)
fr <- DBI::fetch(rs, n=-1)
Any help is greatly appreciated!

The "character" method for dbConnect was removed in the 0.10 release of RMySQL. I'm not sure whether or not this was intentional. It was not mentioned in the release notes.

Related

invalid value from generic function ‘dbListFields’, class “numeric”, expected “character”

I am trying to read table from a database and getting error:
Error in .valueClassTest(ans, "character", "dbListFields") :
invalid value from generic function ‘dbListFields’, class “numeric”, expected “character”
Here is my code:
library(RMySQL)
library(dbConnect)
mydb = dbConnect(MySQL(), user='user',
password='pwd', dbname='blank_copy',
host='IPaddress', port=portnumber)
dbListTables(mydb)
dbListFields(mydb, 'SELECT * FROM tablename')
I don't know why am getting such an error of class for this particular table.
Can anyone help me on this error?
Thanks you.
You need to pass a table name as the second argument for dbListFields, per the documentation.
So you should do something like this:
library(RMySQL)
library(dbConnect)
mydb = dbConnect(MySQL(), user='user',
password='pwd', dbname='blank_copy',
host='IPaddress', port=portnumber)
tables <- dbListTables(mydb)
#tables is a character vector, so you can just pass a subset of that
fields <- dbListFields(mydb, tables[1])

Looping over database connections

I have twelve MySQL database connections created using:
mydb1 = dbConnect(MySQL(), user='user', password=password, dbname='db',host='domain')
mydb2...
mydb3...
...
mydb12...
I have a script where I want to execute the same query on all 12 databases and loop through them. How do I pass the dbConnect objects successfully to a dbSendQuery?
items <- ls()[grep("mydb",ls())]
query <- dbSendQuery(items[1], "SELECT * FROM table")
gives me the error:
Error in (function (classes, fdef, mtable) : unable to find an
inherited method for function ‘dbSendQuery’ for signature
‘"character", "character"’
You cannot pass the textual representation of a connection object to the database functions. Your call is analogous to dbSendQuery("mydb1", "select * from table"), which I'm guessing you would not have typed in literally.
Ultimately you want to deal with a list of connections, which you can form manually with
conns <- list(mydb1, mydb2, ...)
but if that is difficult or you want to be more programmatic about it, try
conns <- lapply(ls()[grep("mydb",ls())], get)
and then
results <- lapply(conns, function(con) dbSendQuery(con, "select * from ..."))

How to use sparklyr spark_write_jdbc to connect to MySql

spark_write_jdbc(members_df,
name = "Mbrs",
options = list(
url = paste0("jdbc:mysql://",mysql_host,":",mysql_port,"/",dbname),
user = mysql_user,
password = mysql_password),
mode = "append")
Results in the following exception:
Error: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
The .jar file is in a folder on the server where RStudio is running, config details below. We're able to access MySql via the RMySql package so MySql is working and accessible.
config$`spark.sparklyr.shell.driver-class-path` <- "/dev/shm/temp/mysql-connector-java-5.1.44-bin.jar"
Even if the question is different, I think the answer still applies also to this:
How to use a predicate while reading from JDBC connection?
Code to connect to JDBC MySQL through sparklyr (I made some slight changes to simplify the code a bit, written by Jake Russ)
library(sparklyr)
library(dplyr)
config <- spark_config()
#config$`sparklyr.shell.driver-class-path` <- "E:\\spark232_hadoop27\\jars\\mysql-connector-java-5.1.47-bin.jar"
#in my case, using RStudio and Sparkly this seemed to be optional
sc <- spark_connect(master = "local")
db_tbl <- spark_read_jdbc(sc,
name = "table_name",
options = list(url = "jdbc:mysql://localhost:3306/schema_name",
user = "root",
password = "password",
dbtable = "table_name"))

Multiple DB connection in R

I was wondering if someone could help with this annoying issue.
I'm trying to create/make multiple connections to different database.
I have a data.frame with 3 connection credentials named conf - It works if I manually enter the connections variable like so:
conn <- dbConnect(MySQL(), user=conf$user, password=conf$passws, host=conf$host, dbname=conf$db)
which ends up creating a single connection.
However, what I want is to be able to refer to the connection as:
conf$conn <- dbConnect(MySQL(), user=conf$user, password=conf$passws, host=conf$host, dbname=conf$db)
here is the error message I'm getting.
Error in rep(value, length.out = nrows) :
attempt to replicate an object of type 'S4'
I think the problem is how I'm adding conf$conn
I used a combination of the pool and config package to solve a similar problem to set up a number of simultaneous PostgreSQL connections. Note that this solution needs a config.yml file with the connection properties for db1 and db2.
library(pool)
library(RPostgreSQL)
connect <- function(cfg) {
config <- config::get(config = cfg)
dbPool(
drv = dbDriver("PostgreSQL", max.con = 100),
dbname = config$dbname,
host = config$host,
port = config$port,
user = config$user,
password = config$password
)
}
conn <- lapply(c("db1", "db2"), connect)

Error in RMySQL package

I am using RMySQL package to write (append) data in current table.
I am using R, version 3.3.2.
My code looks like this:
library(RMySQL)
df_final <- some_data
m<-dbDriver("MySQL")
mydb <- dbConnect(m, user='odvjet12_mislav',
password='my_pass',
host='91.234.46.219',
dbname='odvjet12_fina_pn')
dbWriteTable(mydb, value = df_final, name = "fina_pn", append = TRUE, row.names = FALSE)
This code works fine for some time, but in last ten days, it always return an error:
Error in .local(conn, statement, ...) :
could not run statement: The used command is not allowed with this MySQL version
I don't understand how it is possible for code to work for some time and now, it returns an error?
I kindly ask for feedback on this issue.
Best,
Mislav Šagovac
You could also use dbGetQuery from the RMySQL package and iterate over the rows, which was my solution when I reached a similar error for a dataframe I wanted to write to a MySQL DB:
mydb = dbConnect(MySQL(), user='user', password='password', dbname='databasename', host='hostname')
for(i in 1:nrow(df)){
dbGetQuery(mydb,paste0("INSERT INTO MYTABLE (COL1,COL2) VALUES(",df$col1[i],",",df$col2[i],")"))
}