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 > > 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. > >-- Andreas Borg Abteilung Medizinische Informatik Universit?tsmedizin der Johannes Gutenberg-Universit?t Mainz Institut f?r Med. Biometrie, Epidemiologie und Informatik (IMBEI) Obere Zahlbacher Stra?e 69, 55131 Mainz Tel: +49 (0) 6131 17-5062 E-Mail: andreas.borg at uni-mainz.de
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.
Apparently Analagous Threads
- what is the maximum number of records that we can load using dbwrite
- RSQLite to input dataframe
- Package RPostgreSQL : Problem with dbWriteTable
- Not able to write to PostgreSQL database using "dbWriteTable"
- DBI::dbWriteTable syntax error apparently from quotes