简体   繁体   中英

Using XML in a SQL query

I am trying to query an ACCESS database using R v3.0.3 and am running into repeated SQL syntax errors. My code is as follows.

library(RODBC) 
library(XML)

# Access directory containing the corpus scripts and load all corpus documents    
dale1   <- "C:/Cerberus_Software_Prototype/TR_Corpus/2286newsML.xml")    
doc1    <- xmlInternalTreeParse(dale1)    
xattrs  <- xpathApply(doc1, "//code[@code]", xmlGetAttr, "code")     
xmldf   <- xmlToDataFrame(nodes = getNodeSet(doc1, "//code/code"))      
xattrs1 <- data.frame(xattrs)

# Connect to Accessdb
channel <- odbcConnectAccess("C:/Cerberus_Software_Prototype/R/Economics_data/Cerberus1.mdb")

for (i in 1:6) {
   #  MODIFY SELECT OR ACTION QUERY TO MEET NEEDS, EVEN ADD PARAMS  
   querydf <- sqlQuery(channel, paste0("select All_codes.Description from All_codes 
                                        where All_codes.Code ='", xattrs$[i]Code, "'"))        
 }

close(channel) 

The XML is contained within an XML structure (only pasting the relevant part here) In this example there are six values within the "code code=" block

<code code="ECAT">
    <editdetail attribution="BIP Coding Group" action="confirmed" date="1996-08-20"/>
</code>

Can anyone help with this?

In your SQL statement change the concatenated variable:

xattrs$[i]Code

To

xattrs[i]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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