Search code examples
sqlroracle-sqldeveloperrscript

How to prompt an user for a STUDENT_ID in SQL integrated with R


I want to extend my own post from : How to get user's input of SQL query in R

I have already a sqlQuery, which I want to integerate with R script using the package 'RODBC'

Need help on : I want to prompt a user for a 'STUDENT_ID' and print the results in the console?

Note: At present, I have hard coded the 'STUDENT_ID'

R'

library(RODBC)
library(sqldf)


#DB connections
connection_details<-odbcConnect("db",believeNRows=FALSE)

query <- paste('SELECT STUDENT
   FROM TABLE1
   WHERE STUDENT_ID ='1680',
   AND STUDENT_CLASS = '10')

results= sqlQuery(connection_details, query)
print(results)

Solution

  • This is an answer for your reference.

    query <- paste("SELECT STUDENT
       FROM TABLE1
       WHERE STUDENT_ID ='###ID###' AND STUDENT_CLASS = '10'")
    
    ID <- readline(prompt="Enter ID: ")
    query=gsub("###ID###", ID, query)
    
    results= sqlQuery(connection_details, query)
    print(results)