Skip to content

Unwanted carriage returns storing dataframes with dbWriteTable

2 messages · Hansruedi Baetschmann, Brian Ripley

#
Hi

If I save a dataframe with a character-typed last column to a relational 
database with dbWriteTable, the  values in the last column of the 
resulting table in the database will have a '\r' (carriage return) 
appended. If I read back the dataframe with dbReadTable the last column 
in the resulting dataframe has also '\r' appended (see protocol below). 
Setting or unsetting sql-mode (in the SQL-Server Version 5.0) to or from 
'NO_BACKSLASH_ESCAPE' has no effect on this behavour. How can I avoid 
these unwanted '\r'-tail without a workaround-wrapper, which adds an extra
dummy column before writing and removes it after reading back ? 

I use R.2.7.1 and package RMySQL

Hansruedi
_                           
platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          7.1                         
year           2008                        
month          06                          
day            23                          
svn rev        45970                       
language       R                           
version.string R version 2.7.1 (2008-06-23)
...
$conType
[1] "localhost via TCP/IP"

$serverVersion
[1] "5.0.24-community-nt-log"

$protocolVersion
[1] 10

$threadId
[1] 46

$rsId
list()
+   a=c(1,2,3,4),
+   b=c("a","b","c","d"),
+   c=c("x","y","z","t"),
+   row.names=2)
a c
a 1 x
b 2 y
c 3 z
d 4 t
NULL
[1] TRUE
a   c
1 1 x\r
2 2 y\r
3 3 z\r
4 4 t\r
[1] TRUE

  
    
#
I suspect this is related to the similar problem seen in RSQLite by the 
same author, Cc:ed here :

https://stat.ethz.ch/pipermail/r-help/2007-January/122998.html
https://stat.ethz.ch/pipermail/r-sig-db/2007q1/000248.html

There is a function RMySQL:::mysqlWriteTable that has

    sql4 <- paste("LOAD DATA LOCAL INFILE '", fn, "'",
                   " INTO TABLE ", name,
                   " LINES TERMINATED BY '\n' ", sep="")

I believe that \n needs to be \r\n on Windows. See 
http://dev.mysql.com/doc/refman/5.0/en/load-data.html .  The alternative 
is to alter RMySQL:::safe.write to work with a binary file.
On Tue, 12 Aug 2008, Hansruedi Baetschmann wrote: