How to select all data but the last week in RMySQL with timestamp?
Question
I have this column "timestamp" in table A, and I want to select all data but the last week, here is my R code but an error pop up saying "Error in function (classes, fdef, mtable) : unable to find an inherited method for function "dbWriteTable", for signature "MySQLConnection", "data.frame", "character"", any help would be appreciated? THANK YOU.
fun <- function(con, dat.set, tbl.name) {
if (dbExistsTable(con, tbl.name)) {
BFWeek = dbGetQuery(con, statement=paste("SELECT * FROM A",
"WHERE timestamp < timestampadd(day, -7, now())"))
dbWriteTable(con, BFWeek, tbl.name, row.names=F, append=T);
} else {
dbWriteTable(con, tbl.name, dat.set, row.names=F, append=T);
}
}
fun(conn_table, df, "A")
Solution
The name of the table has to come before the data frame you want to write to that table. In the else
part of your code, you seem to have the order correct, but in the if
part you have BFWeek
before tbl.name
where it should go after.
> showMethods("dbWriteTable")
Function: dbWriteTable (package DBI)
conn="MySQLConnection", name="character", value="character"
conn="MySQLConnection", name="character", value="data.frame"
This would have given you an idea as to which methods are available and which are not.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow