I have been trying to find the reason of the error in this code, but I have not been successful. I would like to pass a variable to SQL, but it keeps on giving the same error.
A MWE is the following (just change user
and password
for the connection).
library(DBI)
library(RMySQL)
library(tidyverse)
data("mtcars")
head(mtcars)
mtcars <- mtcars |>
rownames_to_column("type") |>
as_tibble()
# Open a connection to MySQL
conn <- dbConnect(MySQL(), user = "myuser", password = "mypassword")
# Create the appropriate database
dbGetQuery(conn, "CREATE DATABASE mtcars;")
dbGetQuery(conn, "USE mtcars")
# Query that doesn't work:
dbGetQuery(conn,
"SELECT COUNT(*) FROM mtcars WHERE cyl = ? ",
params = list(8))
This is the error that I keep on getting.
Error in .local(conn, statement, ...) :
unused argument (params = list(8))
I have seen in a post ideas like using \
or \\
to escape ?
but it didn't work. I have tried to search for similar errors, but I haven't find anything remotely similar.
Thank you for any advice you can give me.
RMySQL
does not supportparams=
, and the author (on its github repo) says:Up front, your query should work just fine using the new package. However, I found that loading both
RMySQL
andRMariaDB
breaks things.Docker image setup (since I don't have mysql running anywhere):
In R:
After I install
RMariaDB
in the same running R session, I tried to connect but getOn a whim, I started a new R session and ran that again (no error this time) and then continued:
I'm not certain if the
Id
-error is a bug or just a nuance of having two packages that acknowledge connections to the same type of database. Either way, in a fresh R instance,RMariaDB::MariaDB()
works withparams=
(to the same MySQL database).