RODBC sqlSave 问题
我在使用 RODBC 将数据框插入 mySql 数据库时遇到一些困难。 下面是我正在使用的代码:
data <- data.frame(analysedDataID=c(1,2,3), plateWell=c("a","b","c"), screenPlateOrder=c(1,2,3), wellData=c("A","B","C"))
con <- odbcConnect(DSN, uid="user_id", pwd="some_password")
query <- sqlSave(con, data, tablename = 'wellAnalysedDataTable', rownames=FALSE, append=TRUE)
当我尝试执行此代码时,我收到以下错误消息:
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test, :
missing columns in 'data'
这是我试图将数据插入到的特定表:
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| analysedDataID | int(10) | YES | | NULL | |
| plateWell | varchar(10) | YES | | NULL | |
| screenPlateOrder | int(10) | YES | | NULL | |
| wellData | varchar(30) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
我在这一点上真的很困惑,因为我读过一遍又一遍地查看文档,但就是无法使其发挥作用。任何帮助将不胜感激!
干杯!
谢谢!我添加了您建议的更改,并且案例似乎不再有问题。但是,我不断收到以下错误消息:
'index column(s) analysedDataID plateWell screenPlateOrder wellData not in data frame'
我正在执行的生成此错误的语句是:
sqlUpdate(con, data, tablename="wellAnalysedDataTable", index=c("analysedDataID", "plateWell", "screenPlateOrder", "wellData"))
这对我来说没有任何意义,因为列已经在具有相同列名的数据框中。
I'm having some difficulty inserting a data frame into a mySql database using RODBC.
Below is the code I'm using:
data <- data.frame(analysedDataID=c(1,2,3), plateWell=c("a","b","c"), screenPlateOrder=c(1,2,3), wellData=c("A","B","C"))
con <- odbcConnect(DSN, uid="user_id", pwd="some_password")
query <- sqlSave(con, data, tablename = 'wellAnalysedDataTable', rownames=FALSE, append=TRUE)
When I try to execute this I get the following error message:
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test, :
missing columns in 'data'
Here is the specific table I'm trying to insert the data into:
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| analysedDataID | int(10) | YES | | NULL | |
| plateWell | varchar(10) | YES | | NULL | |
| screenPlateOrder | int(10) | YES | | NULL | |
| wellData | varchar(30) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
I am just really confused at this point as I've read the documentation over and over again but just can't get this to work. Any help would be greatly appreciated!
Cheers!
Thanks! I added the changes you suggested and there does not seem to be a problem with the case anymore. However I keep getting the following error message:
'index column(s) analysedDataID plateWell screenPlateOrder wellData not in data frame'
The statement I'm executing that generates this is:
sqlUpdate(con, data, tablename="wellAnalysedDataTable", index=c("analysedDataID", "plateWell", "screenPlateOrder", "wellData"))
This doesn't make any sense to me as the columns are already in the data frame with the same colnames.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道非常老的问题,但我刚才遇到了(并为我的实例解决了)同样的问题,
从使用
Rodbc 包中得到了这个错误。
我通过设置解决了这个问题
如果其他人从第一个谷歌结果登陆这里,希望这有助于
编辑:
所以我从那以后就更多地研究了这个。
当按照我下面的建议将标志设置为“F”时,您将逐行插入数据库中。
虽然它是“T”,但如果一行不正确,它就会失败,但现在该行失败了,而您却不知道。因此数据库可能丢失了您认为存在的数据。
检查数据库中的行数与您期望从数据框中插入的行数。
您可能有一个不兼容的值。我现在要做的是在循环中使用“粘贴”函数来创建一个字符串,例如“插入 tableName 值(“eg1”,“eg2”)”,这样我就可以获得有关为什么它没有消失的错误消息进入数据库
Very old question I know, but I had (and solved for my instance) the same problem just now, getting this error
from using
In the rodbc package.
I solved it by setting
If anyone else from the first google results lands here, hope this helps
EDIT:
So I since have looked at this more.
When setting the flag to "F" as I recommend below you insert the rows, row by row into the database.
While it was "T" it would fail if one row was incorrect, but now that row fails and you don't know about it. So the data base may be missing data that you thought was in there.
Check the count of rows in the data base vs the number of rows you expect to be inserted from your dataframe.
You probably have a single value that is incompatible. What I have taken to doing now is using "paste" function in a loop to create a string like "insert into tableName values("eg1","eg2") " this way I can get error messages back about why it did not go into the database
不同的错误可能会导致相同的错误消息。因此尚不清楚这是否对您有帮助:
但是,如果“tablename='wellAnalysedDataTablealready' 已在 mySql 数据库中创建为空表,您将收到此错误。尝试将其删除并让 sqlsave(...) 函数处理它
different errors can lead to the same error msg. So it is not clear whether this helps you:
however if "tablename='wellAnalysedDataTablealready' is already created in the mySql database as an empty table you will get this error. Try to remove it and let the sqlsave(...) function take care of it
我遇到了同样的错误,问题是目标表数据类型。即使看起来有可能进行数据转换。
解决步骤是:
设置“verbose=T”对于准确检查问题发生的位置很有用。
I got the same error and the problem was the destination table data types. Even it seems there were possible data conversion.
The solution steps were:
It is useful setting "verbose=T" to check exactly where the problem occurs.
不久前我也遇到过同样的情况,我的问题是由于插入与目标表中的列不匹配的数据帧列号而引起的。所以这就是我所做的。我执行了 SQL 保存,并将附加设置为 false(这意味着创建一个名称与有问题的表不同的新表)。使用工作数据库的 INFORMATION_SCHEMA.COLUMNS 的 table_name 和 column_name 比较这两个表以确定缺少的列。
您可以互换使用 table1 和 table2。一个是 sqlsave 失败的表,而另一个是动态创建的表
I was in the same situation some while back and my problem was rising from inserting data frame column numbers that did not match columns in the destination table. So this what I did. I did an SQL save with an append set to false (implying creation of a new table with a name different from the problematic table). Using the table_name and column_name of the INFORMATION_SCHEMA.COLUMNS of the working database I compared the two tables to determine the missing column.
You can use table1 and table2 interchangeably. one would be the table the sqlsave is failing on while the other would be the one created on the fly
您是否尝试过将数字设为整数并将因子设为字符?
Have you tried making your numerics integers and your factors characters?
如果问题实际上与区分大小写有关,那么看起来有一个简单的修复方法,无需使用“preserve”选项更改任何函数,该选项出现在 Ari B. Friedman 指出的行中:“nochange=cnames”。因此,当您最初创建 RODBC 通道时,您可以简单地将其指定为 case 选项:
If the issue is in fact with case-sensitivity, then it looks like there's an easy fix without changing any functions by using the "preserve" option, which appears in the line noted by Ari B. Friedman: "nochange=cnames". So, when you initially create your RODBC channel, you can simply specify that as the case option: