2010-01-05 12 views
15

में मल्टीलाइन एसक्यूएल क्वेरी आयात करें R में, मैं एक स्ट्रिंग में एक मल्टीलाइन टेक्स्ट फ़ाइल (एसक्यूएल युक्त) की सामग्री कैसे आयात कर सकता हूं?एकल स्ट्रिंग

sql.txt फ़ाइल इस तरह दिखता है:

SELECT TOP 100 
setpoint, 
tph 
FROM rates 

मैं एक अनुसंधान स्ट्रिंग ऐसी है कि वह इस तरह दिखता है कि पाठ फ़ाइल आयात करने की आवश्यकता:

> sqlString 
[1] "SELECT TOP 100 setpoint, tph FROM rates" 

ताकि मैं है कि आरओडीबीसी को इसे

> library(RODBC) 
> myconn<-odbcConnect("RPM") 
> results<-sqlQuery(myconn,sqlString) 

मैंने रीडलाइन कमांड को निम्नानुसार करने की कोशिश की है लेकिन यह gi नहीं है आरओडीबीसी की स्ट्रिंग प्रारूप है।

> filecon<-file("sql.txt","r") 
> sqlString<-readLines(filecon, warn=FALSE) 
> sqlString 
[1] "SELECT TOP 100 "        "\t[Reclaim Setpoint Mean (tph)] as setpoint, " 
[3] "\t[Reclaim Rate Mean (tph)] as tphmean "  "FROM [Dampier_RC1P].[dbo].[Rates]"   
> 

उत्तर

17

बहुमुखी paste() आदेश की कोशिश है कि तर्क collapse="" के साथ क्या कर सकते हैं:

lines <- readLines("/tmp/sql.txt") 
lines 
[1] "SELECT TOP 100 " " setpoint, "  " tph "   "FROM rates"  

sqlcmd <- paste(lines, collapse="") 
sqlcmd 
[1] "SELECT TOP 100 setpoint, tph FROM rates" 
+1

धन्यवाद डिर्क - यह काम करता है, स्ट्रिंग को छोड़कर यह "दृश्य शीर्ष 100 \ t सेटपॉइंट, \ t tph \ t दरों से \ t" जैसा दिखता है। बस gsub ("\ t", "", sqlcmd) –

+0

जोड़ने के लिए जरूरी है, मैंने जो भी कॉपी किया था, वह टैब नहीं था, किसी भी स्थिति में एसक्यूएल पार्सर शायद टैब को अनदेखा कर देगा और आपको 'gsub() '- अच्छा लगता है । –

+6

यदि आपके पास कोई टिप्पणी है तो यह आपकी क्वेरी को बुझाने की संभावना है, है ना? मैं 'पेस्ट (रीडलाइन (' pathto/query.sql ') का उपयोग करता हूं, collapse = "\ n") ' –

4

यहाँ मैं क्या उपयोग कर रहा हूँ के अंतिम संस्करण है। धन्यवाद Dirk।

fileconn<-file("sql.txt","r")   
sqlString<-readLines(fileconn)   
sqlString<-paste(sqlString,collapse="") 
gsub("\t","", sqlString) 
library(RODBC) 
sqlconn<-odbcConnect("RPM") 
results<-sqlQuery(sqlconn,sqlString) 
library(qcc) 
tph <- qcc(results$tphmean[1:50], type="xbar.one", ylim=c(4000,12000), std.dev=600) 
close(fileconn) 
close(sqlconn) 
0

मैं sql <- gsub("\n","",sql) और sql <- gsub("\t","",sql) एक साथ उपयोग करें।

8

नीचे एक आर फ़ंक्शन है जो एक बहुभाषी SQL क्वेरी (टेक्स्ट फ़ाइल से) में पढ़ता है और इसे एकल-पंक्ति स्ट्रिंग में परिवर्तित करता है। फ़ंक्शन स्वरूपण और पूर्ण-रेखा टिप्पणियों को हटा देता है।

इसके इस्तेमाल के लिये कार्यों को परिभाषित करने के लिए कोड को चलाने के लिए, और अपने एकल लाइन स्ट्रिंग ONELINEQ चलाने का परिणाम होगा ("querytextfile.sql" "~/path// thefile को")।

यह कैसे काम करता है: इनलाइन टिप्पणियों का विवरण यह है; यह प्रश्न के प्रत्येक पंक्ति संस्करण को लिखने के लिए आवश्यक नहीं है (प्रश्न के लिए पूछे जाने वाले) की क्वेरी और हटाए गए प्रत्येक चीज को बदलता है (कुछ भी नहीं बदला जाता है)। नतीजा लाइनों की एक सूची है, जिनमें से कुछ खाली हैं और फ़िल्टर किए गए हैं; अंतिम चरण इस (असूचीबद्ध) सूची को एकसाथ पेस्ट करना और एकल पंक्ति को वापस करना है।

#
# This set of functions allows us to read in formatted, commented SQL queries 
# Comments must be entire-line comments, not on same line as SQL code, and begun with "--" 
# The parsing function, to be applied to each line: 
LINECLEAN <- function(x) { 
    x = gsub("\t+", "", x, perl=TRUE); # remove all tabs 
    x = gsub("^\\s+", "", x, perl=TRUE); # remove leading whitespace 
    x = gsub("\\s+$", "", x, perl=TRUE); # remove trailing whitespace 
    x = gsub("[ ]+", " ", x, perl=TRUE); # collapse multiple spaces to a single space 
    x = gsub("^[--]+.*$", "", x, perl=TRUE); # destroy any comments 
    return(x) 
} 
# PRETTYQUERY is the filename of your formatted query in quotes, eg "myquery.sql" 
# DIRPATH is the path to that file, eg "~/Documents/queries" 
ONELINEQ <- function(PRETTYQUERY,DIRPATH) { 
    A <- readLines(paste0(DIRPATH,"/",PRETTYQUERY)) # read in the query to a list of lines 
    B <- lapply(A,LINECLEAN) # process each line 
    C <- Filter(function(x) x != "",B) # remove blank and/or comment lines 
    D <- paste(unlist(C),collapse=" ") # paste lines together into one-line string, spaces between. 
    return(D) 
} 
# TODO: add eof newline automatically to remove warning 
############################################################################################# 
+0

... आप जानते हैं, यह शायद अब तक का सबसे अच्छा आत्म-प्रचार उत्तर है जिसे मैंने अभी देखा है। क्या आप एक संक्षिप्त स्पष्टीकरण जोड़ सकते हैं कि यह आपके उत्तर में कैसे काम करता है? (और इसके द्वारा, मेरा मतलब है [अपना जवाब संपादित करें] (http://stackoverflow.com/posts/30551944/edit)) –

2

यह मैं क्या उपयोग है:

# Set Filename 
fileName <- 'Input File.txt' 

doSub <- function(src, dest_var_name, src_pattern, dest_pattern) { 
    assign(
      x  = dest_var_name 
     , value = gsub(
          pattern  = src_pattern 
         , replacement = dest_pattern 
         , x = src 
        ) 
     , envir = .GlobalEnv 
    ) 
} 


# Read File Contents 
original_text <- readChar(fileName, file.info(fileName)$size) 

# Convert to UNIX line ending for ease of use 
doSub(src = original_text, dest_var_name = 'unix_text', src_pattern = '\r\n', dest_pattern = '\n') 

# Remove Block Comments 
doSub(src = unix_text, dest_var_name = 'wo_bc_text', src_pattern = '/\\*.*?\\*/', dest_pattern = '') 

# Remove Line Comments 
doSub(src = wo_bc_text, dest_var_name = 'wo_bc_lc_text', src_pattern = '--.*?\n', dest_pattern = '') 

# Remove Line Endings to get Flat Text 
doSub(src = wo_bc_lc_text, dest_var_name = 'flat_text', src_pattern = '\n', dest_pattern = ' ') 

# Remove Contiguous Spaces 
doSub(src = flat_text, dest_var_name = 'clean_flat_text', src_pattern = ' +', dest_pattern = ' ') 
1

यह readChar() बजाय readLines() उपयोग करने के लिए संभव है। मिश्रित टिप्पणी (-- या /* */) के साथ मेरे पास एक सतत समस्या थी और यह हमेशा मेरे लिए अच्छा काम करता रहा है।

sql <- readChar(path.to.file, file.size(path.to.file)) 
query <- sqlQuery(con, sql, stringsAsFactors = TRUE) 
संबंधित मुद्दे