Im having a dataframe that looks like this:
city <- c("Alkmaar", "Den Haag")
df <- data.frame(city)
What I would like to do now is write values into a mysql database. Therefore I do the following:
library(DBI)
con <- dbConnect(RMySQL::MySQL(),
dbname = "match_predictions",
host = "109.237.221.34",
port = 3306,
user = "**",
password = "***")
for(i in 1:nrow(df)){
var1 <- city[i]
dbGetQuery(con, "INSERT INTO weather(city) VALUES(var1)")
}
But this trows an error:
Error in .local(conn, statement, ...) :
could not run statement: Unknown column 'var1' in 'field list'
Any thoughts on how i can insert a variable into the database?
sprintf("INSERT INTO weather(city) VALUES(%s)", var1). Ifvar1is a vector of cities, a vector of queries will be returned (one for each city)."INSERT INTO weather(city) VALUES(Alkmaar)". In your case, I don't know whatxis.