简体   繁体   English

RODBC :: sqlQuery检测隐藏的字符

[英]RODBC::sqlQuery detecting hidden characters

I am struggling with hidden characters when querying an Oracle database using RODBC. 使用RODBC查询Oracle数据库时,我在隐藏字符方面苦苦挣扎。 First my code: 首先我的代码:

library(RODBC)
con <- odbcConect('dsn', uid = 'user', pwd = 'pass')
query <- read_file('Query.sql')
query <- gsub('\n', ' ',gsub('\t', ' ',gsub('\r' ,' ', query)))

I'm using gsub here to manually remove the three hidden characters I had identified in my sql file. 我在这里使用gsub来手动删除我在sql文件中标识的三个隐藏字符。

df <- sqlQuery(con, query = query)

This is returning a list of two errors. 这将返回两个错误的列表。

[1] "HY000 911 [Oracle][ODBC][Ora]ORA-00911: invalid character\n" 

and

[2] "[RODBC] ERROR: Could not SQLExecDirect...

Initially I was copy and pasting this query from outlook into a text file. 最初,我是从Outlook复制此查询并将其粘贴到文本文件中。 Then I retyped the entire thing in hopes that would get rid of the hidden characters. 然后我重新输入了整个内容,希望能消除隐藏的字符。 Now I'm using a string of gsubs to manually remove the hidden characters and I'm still receiving the error. 现在,我使用一串gsubs手动删除隐藏的字符,但仍然收到错误。 Looking through the 'query' vector I don't see any hidden characters so I'm not sure where the issue is coming from. 查看“查询”向量时,我看不到任何隐藏的字符,因此我不确定问题出在哪里。

I've read that RODBC can struggle with aggregating in SQL queries but this query only uses LEFT JOIN, CASE and WHERE for higher level keywords. 我已经读到RODBC很难在SQL查询中进行聚合,但是此查询仅对更高级别的关键字使用LEFT JOIN,CASE和WHERE。

Any help is appreciated. 任何帮助表示赞赏。

Perhaps convert everything to ASCII, using base:iconv() . 也许使用base:iconv()将所有内容转换为ASCII。

If the query itself has bad characters, I would start by hand with a simple SELECT query with one column; 如果查询本身包含错误字符,我将首先从一个简单的SELECT查询开始,该查询只有一列; start with a fresh text file where you know the encoding is well-behaved. 从一个新的文本文件开始,在该文件中您知道编码是正确的。 Make sure the query works in Oracle Developer (or something that doesn't depend on R). 确保查询可在Oracle Developer中运行(或不依赖R的查询)。 Then make sure the the RODBC connection works with that simple query. 然后,确保RODBC连接可与该简单查询一起使用。

Assuming you must use sql files that you didn't write from scratch, you probably want to work your way in this direction. 假设您必须使用不是从头开始编写的sql文件,那么您可能想朝这个方向努力。

content   <- readr::read_file("Query.sql")
cleaned   <- base::iconv(x=content, from="latin1", to="ASCII//TRANSLIT", sub="&")

if( grepl("&", cleaned) ) {
  cat(cleaned)
  stop("The query might contain non-ASCII characters with no good non-ASCII equivalent.  Check the console for the '&' substitution character.")
}

con <- odbcConect('dsn', uid = 'user', pwd = 'pass')
df  <- sqlQuery(con, query = returned_value)

I'm throwing an error above when there's likely a bad character, since it might not be clear what should replace it. 当可能出现不良字符时,我会在上面抛出错误,因为可能尚不清楚应该替换什么字符。

Even if that's not your ultimate desired solution, collapsing everything to ASCII could help confirm your suspicion. 即使这不是您最终想要的解决方案,将所有内容折叠为ASCII也可以帮助您确认怀疑。 Also, consider using Notepad++ or Atom to show nonprinting characters, like in https://stackoverflow.com/a/8523118/1082435 . 另外,考虑使用Notepad ++或Atom来显示非打印字符,例如https://stackoverflow.com/a/8523118/1082435

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

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