4

I want to be able to add a column into an existing table with its corresponding type.

This is how I tried it:

library("RMySQL")


# Connect to DB
v_db <- dbConnect(MySQL(),
              user="USERNAME", password="PASSWORD",
              dbname="DBNAME", host="localhost")
on.exit(dbDisconnect(v_db))

#Read in my new data (into R)
newcolumn <- read.csv("test.csv")
newcolumn
id   datafornewcolumn
1    4
2    5 
3    8

dbq <- dbSendQuery(v_db, "SELECT * FROM `EXISTINGTABLE`")
dbq <- fetch(dbq, n = -1)
dbq
id  existing columns
1   ...
2   ...
3   ...

dbWriteTable(v_db, "EXISTINGTABLE", merge(dbq, newcolumn, by="id", all.x=TRUE), row.name=FALSE, overwrite=T) 

But with that last statement I overwrite the existing table with the new one thereby losing all the corresponding variable types.

Then I tried a workaround. Write the new data into a new table in SQL and after that merge that into the EXISTINGTABLE. However, it seems I'm not able to do that corretly:

dbSendQuery(v_db, "create table workaround (id int not null primary key, 
        newcolumn DECIMAL(3,1))")

#write data into that new empty table called workaround --> works fine
dbWriteTable(v_db, "workaround", neu, row.name=FALSE, append=TRUE)

#something works...
dbSendQuery(v_db, "SELECT * FROM EXISTINGTABLE
     LEFT JOIN workaround ON EXISTINGTABLE.id = workaround.id
     UNION
     SELECT * FROM EXISTINGTABLE
     RIGHT JOIN workaround ON EXISTINGTABLE.id = workaround.id")
<MySQLResult:(29344,26,2)> 

The result should look like this:

 EXISTINGTABLE
 id  existingcolumns datafornewcolumn
 1   ...             4 
 2   ...             5
 3   ...             8
Thomas
  • 1,392
  • 3
  • 22
  • 38

2 Answers2

1

Have you tried to modify your table using SQL with a ALTER TABLE statement?

rs <- dbSendStatement(v_db, "ALTER TABLE table_name ADD COLUMN [...]")
dbHasCompleted(rs)
dbGetRowsAffected(rs)
dbClearResult(rs)

Then you can simply send and UPDATE statement to add new values.

Try first with dbExecute() method... and try this on R Console:

?dbExecute or ?dbSendStatement

Check this out too: RMySQL dbWriteTable with field.types

Community
  • 1
  • 1
eddy85br
  • 284
  • 4
  • 12
0

Adding a column to an existing table can be done as :

library(dbConnect) # *loads the packages Loading required package: RMySQL
                                        Loading required package: DBI
                                        Loading required package: methods
                                        Loading required package: gWidgets*


mydb =dbConnect(MySQL(),user='root',password='newpass',dbname='database_name', host='localhost')

my_table <- dbReadTable(conn=mydb,name='table_name') 
my_table$column_name<- NA # *creates new column named column_name and populates its value NA*
head(my_table) # *shows the data* 
Scorpio
  • 2,309
  • 1
  • 27
  • 45
Jaba
  • 25
  • 7
  • It seems that my question was ambiguous. Sorry for that. I do know how to do that in R. However, I'd like to know how I can do that in MySQL from within R... – Thomas Sep 24 '15 at 08:34