[R-sig-DB] RODBC sqlSave appends rows from data frame in reverse order

Chris c@ @end|ng |rom |ncent|ve-|nve@t|ng@com
Wed Nov 20 15:46:58 CET 2013


Hi,

 

I append rows to a table in MySQL with sqlSave from RODBC package. The data
frame that is saved is ordered by date ascending (first column) as is the
table in the DB. When I send the whole data frame at once, the data frame is
appended in reverse order. That means the row with the newest date in the
data frame is the first row appended to the table, while the row with oldest
date will be last row in the table. OK, I thought that "append" just orders
by date descending and that is why the reverse ordering happens. But when I
send each row of the data frame using a for-loop the result is the same: The
first row send is the last row in the table, the second row send, is the
second last row in the table.

My solution is to run the for loop from last to first row of the data frame
but I am looking for an explanation why append works like this and if it's
possible to change this in some setting. 

 

#Send data frame completely (6 rows, 12 cols)

sqlSave(con, df, tablename = table, rownames=FALSE, addPK=TRUE, append=TRUE,
verbose=TRUE)

 

# Send data frame row by row

for (a in 1:6) {

      dfsend <- df[a,]

      sqlSave(con, dfsend, tablename = table, rownames=FALSE, addPK=TRUE,
append=TRUE, verbose=TRUE) 

    }

 

# "solution": Send data frame row by row, starting from last row of data
frame

for (a in 6:1) {

      dfsend <- df[a,]

      sqlSave(con, dfsend, tablename = table, rownames=FALSE, addPK=TRUE,
append=TRUE, verbose=TRUE) 

    }

 

Thank you

Chris


	[[alternative HTML version deleted]]




More information about the R-sig-DB mailing list