简体   繁体   English

R中的sqlSave创建并将数据帧保存到sql表

[英]sqlSave in R to create and save a dataframe to an sql table

Hi I am using R to save a data frame into a DB2 SQL table. 您好我使用R将数据帧保存到DB2 SQL表中。 I seem to be able to create the table skeleton, but not append the data to the table - 我似乎能够创建表骨架,但不会将数据附加到表 -

>df <- read.csv("dat.csv")

where dat.csv is a csv with no headers, just raw data in two columns 其中dat.csv是没有标题的csv,只是两列中的原始数据

then i create the table: 然后我创建表:

>sqlQuery(channel, "create table sqltable 
                   (
                   col1  int,
                   col2  float
                   )"
         (

where I confirm the table is created by being able to select the empty table "sqltable" on the database 我确认表是通过能够在数据库上选择空表“sqltable”来创建的

so now I need to add the data from "dat.csv" into "sqltable" by doing: 所以现在我需要通过执行以下操作将“dat.csv”中的数据添加到“sqltable”中:

>sqlSave(channel, df, "sqltable", verbose=T, fast=T, append=T)

no: 1 rownames 1/***/no: 2 col1 31105/***/no: 3 col2 0.001/***/
no: 2 rownames 1/***/no: 2 col1 31106/***/no: 3 col2 0.023/***/
no: 3 rownames 1/***/no: 2 col1 31107/***/no: 3 col2 1.456/***/
no: 4 rownames 1/***/no: 2 col1 31108/***/no: 3 col2 0.001/***/
no: 5 rownames 1/***/no: 2 col1 31109/***/no: 3 col2 2.102/***/

all seems good until I do: 一切都好看,直到我这样做:

>sqlQuery(channel,"select * from sqltable")

[1] COL1     COL2
<0 rows> or 0-length row.names

the sqlSave command clearly picks up the data from dat.csv, so why is it not added to the table? sqlSave命令清楚地从dat.csv中获取数据,为什么它没有添加到表中? what am I doing wrong? 我究竟做错了什么?

It would be a good idea to define varTypes within sqlSave. 在sqlSave中定义varTypes是个好主意。 Here's how I do it. 这是我如何做到的。 :-) :-)

columnTypes <- list(ColumnName="varchar(255)", NumberColumn="float", datecolumn="date")

This defines the column types for you data so you can add the argument within sqlSave. 这将为您的数据定义列类型,以便您可以在sqlSave中添加参数。 So lets do the sqlSave now. 所以现在让我们来做sqlSave。

sqlSave(channel, YourTable, rownames = FALSE, varTypes = columnTypes)

and if the table has already been added then I would do an sqlDrop before that. 如果表已经添加,那么我会在此之前做一个sqlDrop。

Why is this an answer? 为什么这是一个答案?

Well, I had the same problem. 好吧,我遇到了同样的问题。 I was getting an error saying it couldn't execute the sqlSave but it was putting in my columns in the database. 我收到一个错误,说它无法执行sqlSave,但它放在数据库中的列中。 So I had to fix the columnTypes. 所以我不得不修复columnTypes。 R tries to do it manually but it's not perfect. R试图手动完成,但并不完美。 :-) :-)

Use rownames = FALSE inside your sqlSave() . sqlSave()使用sqlSave() rownames = FALSE

Since you do not provide a sample of the data from dat.csv, I am assuming it has 2 columns based on your sqlSave code block. 由于您没有提供dat.csv中的数据样本,因此我假设它基于您的sqlSave代码块有2列。 From your create statement, sqltable has only 2 columns; 从你的create语句中,sqltable只有2列; sqlSave defaults to rownames = TRUE which will make the data you enter from the dataframe have 3 columns, instead of the 2 you desire. sqlSave默认为rownames rownames = TRUE ,这将使您从数据框输入的数据有3列,而不是您想要的2列。 By attempting to place 3 columns into a Microsoft SQL Server table with 2 columns without rownames = FALSE my R session gets aborted with a fatal error. 通过尝试将3列放入具有2列但没有rownames = FALSE的Microsoft SQL Server表中,我的R会话因为致命错误而中止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM