Skip to content

help in dbWriteTable

3 messages · arunkumar1111, Andreas Borg, David M. Schruth

#
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.
#
Hello,

you can fetch the column names of a table with dbListFields and then 
reorder or rename the data frame according to those.

If you want more specific help, provide an example (RSQLite would be a 
good choice as database engine to make it easily reproducible for others).

Best regards,

Andreas

arunkumar1111 schrieb:

  
    
#
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: