简体   繁体   English

sqlSave 错误:找不到表

[英]sqlSave error: table not found

I'm trying to save a R dataframe back to a sql database with the following code:我正在尝试使用以下代码将 R dataframe 保存回 sql 数据库:

    channel <- odbcConnect("db")
    sqlSave(db, new_data, '[mydb].[dbo].mytable', fast=T, rownames=F, append=TRUE)

However, this returns the error "table not found on channel", while simultaneously creating an empty table with column names.但是,这会返回错误“在通道上找不到表”,同时创建一个包含列名的空表。 Rerunning the code returns the error "There is already an object named 'mytable' in the database".重新运行代码会返回错误“数据库中已经有一个名为‘mytable’的 object”。 This continues in a loop - can someone spot the error?这将继续循环 - 有人可以发现错误吗?

Is this about what your data set looks like? 这是关于你的数据集是什么样的?

MemberNum  x             t.x T.cal m.x T.star h.x h.m.x e.trans e.spend       
1          2.910165e+12  0   0     205 8.77   52  0     0       0.0449161  

I've had this exact problem a few times. 我有几次这个确切的问题。 It has nothing to do with a table not being found on the channel. 它与在频道上找不到的表无关。 From my experience, sqlSave has trouble with dates and scientific notation. 根据我的经验,sqlSave在日期和科学记数法方面存在问题。 Try converting x to a factor: 尝试将x转换为因子:

new_data$x = as.factor(new_data$x)

and then sqlSave. 然后是sqlSave。 If that doesn't work, try as.numeric and even as.character (even though this isn't the format that you want. 如果这不起作用,请尝试as.numeric甚至as.character (即使这不是您想要的格式。

As a first shot try to run sqlTables(db) to check the tables in the db and their correct names. 作为第一次尝试运行sqlTables(db)来检查sqlTables(db)的表及其正确的名称。 You could then potentially use this functions return values as the input to sqlSave(...) 然后你可以使用这个函数返回值作为sqlSave(...)的输入

It seems you are trying to write to a SQL Server. 您似乎正在尝试写入SQL Server。 If you specify the database name in the ODBC connection, and then refer to the table as "dbo.mytable" it might help. 如果在ODBC连接中指定数据库名称,然后将该表称为“dbo.mytable”,则可能有所帮助。

I could do it changing the connection in the driver odbc. When you open it, you can do it for one db or in general for all dbs.我可以更改驱动程序 odbc 中的连接。当您打开它时,您可以为一个数据库或通常为所有数据库执行此操作。 When you opened it for one db, you will not have a problem with sqlSave().当您为一个数据库打开它时,sqlSave() 不会有问题。

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

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