Вопрос

I'm trying to save a R dataframe back to a sql database with the following code:

    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". 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. Try converting x to a factor:

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

and then sqlSave. If that doesn't work, try as.numeric and even as.character (even though this isn't the format that you want.

Другие советы

As a first shot try to run sqlTables(db) to check the tables in the db and their correct names. You could then potentially use this functions return values as the input to sqlSave(...)

It seems you are trying to write to a SQL Server. If you specify the database name in the ODBC connection, and then refer to the table as "dbo.mytable" it might help.

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. When you opened it for one db, you will not have a problem with sqlSave().

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top