0

I have a dataframe that I would like to upload to a cloud environment. Im already connected to the database and now would like to "write away" the records.

I have a dataframe that looks like this:

#df
a  b
1 3 11
2 4 12

And try to add it to the table "team" on the server like this:

 #connect to database (conection is called "con")

 for (i in nrow(df)){

  var1 <- df$a[i]
  var2 <- df$b[i]
  players_home <- dbGetQuery(con, "INSERT INTO team VALUES (var1, var2)")

 }

This however gives me the following error:

  Error in .local(conn, statement, ...) : 
  could not run statement: Unknown column 'var1' in 'field list' 

Any thoughts on how I can get this working?

Wistar
  • 3,770
  • 4
  • 45
  • 70
John Dwyer
  • 189
  • 2
  • 13

1 Answers1

0

You need to specify your columns for an insert statement

Some instructions here

INSERT INTO table_name (column1, column2, column3,...) 
VALUES (value1,value2, value3,...)

You should also use paste otherwise r assumes your passing a string as argument. See this question

In your case, assuming that your values match the column numbers, it would be

query <- paste("INSERT INTO team VALUES(",var1, ",", var2, ")")
players_home <- dbGetQuery(con, query)
Community
  • 1
  • 1
Wistar
  • 3,770
  • 4
  • 45
  • 70