[R] help in dbWriteTable

David M. Schruth dschruth at u.washington.edu
Fri Dec 2 18:46:27 CET 2011


Hi,

The following code should work:

fields <- dbListFields(con, db.table.name)
reordered.names <- names(df)[match(fields, names(df))]
df <- df[ ,reordered.names]

But, you might want to try using the function 'dbWriteTable2' in the 
'caroline' package. (In fact the three lines above have been copied 
verbatim out of said function). It works much like the original 
dbWriteTable but also addresses the column reordering frustration you 
mention and more: na's in NOT NULL columns, length mismatches, adding NA 
columns for missing fields, type checking as well as primary key support 
for PostgreSQL.

I use it mainly with Postgres so I can't say for sure if it'll work for 
you.  But let me know if it doesn't!

-Dave Schruth

On 12/1/2011 8:53 PM, arunkumar1111 wrote:
> hi
>
>   I need some help in dbWriteTable.
> I'm not able to insert the rows in the table if the column order are not
> same in the database and in the dataframe which i'm inserting. Also facing
> issue if the table is already created externally and inserting it thru
> dbWrite.
>
> is there some way that we can sepecify the rownames in the dbwrite..or any
> method which will solve my problem
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/help-in-dbWriteTable-tp4145110p4145110.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list