RODBC queries returning zero rows

依然范特西╮ 提交于 2019-11-27 19:46:53

It turns out that all I needed to do was to set rows_at_time = 1 in addition to believeNRows = FALSE while setting up my ODBC connection.

piconn <- odbcConnect(dsn = "PI", uid = "pwd", believeNRows = FALSE, rows_at_time = 1)
sqlStr <- "SELECT tag, time, status, value FROM piinterp WHERE tag = 'RV1.MADST101_WINDSPEED' and time > DATE('-12h') and timestep = '+2m'"    
results <- sqlQuery(piconn, sqlStr)

Try adding

believeNRows = FALSE

to the query. This is an issue that has come up with a few of the drivers which report a wrong size on the result set.

  1. Test if the ODBC driver works correctly. Each ODBC driver should provide simple means to test the connection. Also try to connect to the ODBC source using MS Office (Access, Excel...) or Open Office.
  2. If the above works, then go to R and try the simplest query possible, like select 1 (your query isn't the simplest, as darcken noted! You have to try really the simplest query to be sure).
  3. If it doesn't work, try to call odbcGetErrMsg() function after each RODBC function call (after connect, after query, ...).

I had the same problem and fixed it by adding "rows_at_time=1" to the odbcConnect call. From the odbcConnect help:

*Several errors which have been reported as bugs in RODBC 1.3-0 which were in fact ODBC driver errors that can be circumvented by setting rows_at_time = 1 (and the warning under that argument has always been there). The drivers involved have been third-party Oracle drivers and old SQL Server drivers.*

In my case I was running 64bit R 2.15.0, RODBC 1.3-5 and the Actual ODBC Oracle driver on OS X Lion.

I think you need to rule out that your actually connecting to the database/table first by getting SELECT * FROM MYTABLE to work within R. If you can't get this working then something is wrong with your setup/drivers.

Once you are sure that you can actually query the database/table within R then progressively make your query more complex and try to isolate where the issue is. One thing to try might be double == on your equality conditions.

I've tried using a bunch of databases(sql server,mysql,sqlite) within R and the performance has been poor with all of them. Imo your better off querying the database natively, dumping to text, then reading the file into R.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!