How can I store MySQL database results in R dataframe - mysql

I'm creating a shiny app in which I need to create a plot from the data returned by the sql query. Now I'm trying to do this by creating a dataframe and storing the value in it. When I run this shiny app it gives me an error cannot coerce class "structure("MySQLResult", package = "RMySQL")" to a data.frame
How can I store the database query result in a dataframe.

If you work with dplyr, you can use dplyr::collect() to save query results into the data frame. Please visit RStudio website on working with databases to see more ways to do it.

I am not sure I got your question right so correct me if I am wrong.
This is what am thinnking:
frame <- dbGetQuery(con, statement= paste("select col1
from table1")) `
con is your dB connection.
Convert year into a dataframe:
year_new<-data.frame(year)
If I have not answered your question, let me know.
Also kindly post how you are doing it so it will be easier to understand what the problem is.`

Related

How to manipulate/clean data located in a MySQL database using base R commands?

I've connected to a MySQL database using the RMariaDB package, and, thanks to the dbplyr package, am able to adjust the data using dplyr commands directly from R studio. However, there are some basic things I want to do that require base R functions (there are no equivalents in dplyr to my knowledge). Is there a way to clean this data using base R commands? Thanks in advance.
The answer to this arises from how the dbplyr package works. dbplyr translates certain dplyr commands into SQL. For example:
library(dplyr)
library(dbplyr)
data(mtcars)
# setup simulated database connection
df_postgre = tbl_lazy(mtcars, con = simulate_postgres())
# fetch first 5 records
first_five = df_postgre %>% head(5)
# view SQL transaltion
first_five %>% show_query()
# resulting SQL translation
<SQL>
SELECT *
FROM `df`
LIMIT 5
The major constrain for this approach is that dbplyr can only translate certain commands into SQL. So something like the following will fail:
# setup simulated database connection
df_postgre = tbl_lazy(mtcars, con = simulate_postgres())
# fetch first 5 records
first_five = df_postgre[1:5,]
While head(df, 5) and df[1:5,] produce identical output for data.frames in local R memory, dbplyr can not translate developer intention, only specific dplyr commands. Hence these two commands are very different when working with database tables.
The other element to consider here is that databases are primarily read-only. In R we can do
df = df %>%
mutate(new_var = 2*old_var)
and this changes the data held in memory. However in databases, the original data is stored in the database and it is transformed based on your instructions when it is requested. There are ways to write completely new database tables from existing database tables - there are already several Q&A on this under the dbplyr tag.

How to get XML from MYSQL using Python Flask?

I got a MYSQL database and a table in it. I want to convert this table into XML and be able to access the data in it using GET method, but the problem I am facing is, I want to know is it possible to use the Python Flask for this process to convert the table to an XML and store it in the memory (like JSON), can someone help me to give an idea of how to achieve this.
Thanks,
First get your results as a dictionary. Then apply dicttoxml to it, as in the following example:
import MySQLdb
import MySQLdb.cursors
import dicttoxml
conn = MySQLdb.Connect(
host='localhost', user='user',
passwd='secret', db='test')
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cursor.execute("SELECT col1, col2 FROM tbl")
rows = cursor.fetchall()
xml = dicttoxml.dicttoxml({'results': rows}, attr_type=False)
Hope that helps...

How to fix dbWriteTable error "unable to find an inherited method for function 'dbWriterTable' for signature...?"

I want to insert data in MySQL from a dataframe in R. I managed to connect without problems from R to MySQL using dbConnect, however when I try to insert my data using dbWriteTable I keep getting the error
unable to find an inherited method for function 'dbWriterTable' for signature '"integer", "character", "data.frame"'.
Now, I've tried the proposed solution mentioned here How to resolve this error--dbWriteTable() but this solution doesn't work for me. My original code was
dbWriteTable(conn, "Date", france$Date)
because my table in MySQL is called Date and my dataframe in R is called france and has a column Datecontaining dates (the type of this column is date too). After the proposed solution, my code becomes
dbWriteTable(conn, "Date", data.frame(dat=france$Date), row.names=FALSE, append=TRUE)
but I'm getting the same error. I tried adding field.types=list("date") as mentioned in the following solution RMySQL dbWriteTable with field.types but I get the same error.
Finally, I've tried to use dbSendQuery together with paste() to insert manually my data as suggested here How to insert integer values with query in MySQL in R? but I get the same error again!
This is driving me crazy. Any help will be appreciated.
I got the same error because the object I was providing to dbWriteTable() wasn't a data.frame.
To fix it, I simply convert the object to a data.frame first
dat <- as.data.frame(dat)
dbWriteTable(con, "mydbtable", dat)
Did you try connecting to your database? I was having the same error and when I checked connection, it was broken. Make sure you check the connection to db and run the dbWriteTable command next. It should work

How can I connect to a MySQL database into Apache Spark using SparkR?

I am working on Spark 2.0 and SparkR libs. I want to get a sample code on how can I do following things in SparkR?
Connect to a MySQL or any other SQL database using SparkR.
Write SQL queries like SELECT , UPDATE etc. to modify a table in that database.
I know to do it using R. However I would need some help to use Spark Sessions or SparkSQL context. I am using R Studio for the development.
Moreover, how do we submit this R code as Spark Batch to run continuously at a regular intervals?
jdbcurl <- "jdbc:mysql://xxx.xxx.x.x:xxxx/database"
data <- read.jdbc(jdbcurl, "tablename", user = "user", password = "password" )

How to parsimoniously refer to a data frame in RMySQL

I have a MySQL table that I am reading with the RMySQL package of R. I would like to be able to directly refer to the data frame stored in the table so I can seamlessly interact with it rather than having to execute RMySQL statement every time I want to do something. Is there a way to accomplish this? I tried:
data <- dbReadTable(conn = con, name = 'tablename')
For example, if I now want to check how many rows I have in this table I would run:
nrow(data)
Does this go through the database connection, or am I now storing the object "data" locally, defeating the whole purpose of using an external database?
data <- dbReadTable(conn = con, name = 'tablename')
This command downloads all the data into a local R dataframe (assuming you have enough RAM). Any operations with data from that point forward do not require the SQL connection.