简体   繁体   English

用于将textInput值传递给MYSQL查询的语法

[英]Syntax for passing textInput value to MYSQL query

I am trying to pass the value of textInput into MySQL query. 我试图将textInput的值传递给MySQL查询。

The first question is what is the right syntax for the LIKE operator combined with textInput, when using MySQL as the driver? 第一个问题是使用MySQL作为驱动程序时,将LIKE运算符与textInput结合使用的正确语法是什么? Where should I put the %% in the query statement? 我应将%%放在查询语句的何处?

Second is how best can I capture the reactivity of the textInput, to be able to pass it to the query and create a table? 其次,如何最好地捕获textInput的反应性,以便将其传递给查询并创建表?

  library(shiny)
  library(DBI)
  library(RMySQL)

  server <- shinyServer(function(input, output, session) {
                con <- dbConnect(MySQL(), user='user', port = 3306, password='pwd', dbname='db', host='host' )

               on.exit(dbDisconnect(con), add = TRUE) 

      output$tableview <- renderTable({

             con <- dbConnect(MySQL(), user='user', port = 3306, password='pwd', dbname='db', host='host' )

             on.exit(dbDisconnect(con), add = TRUE)


           table <- reactive({
                    dbGetQuery(con, statement = 
                    paste0(" SELECT author,  title, publicationDate,  FROM publications  
                   WHERE publications.abstract LIKE %'",input$textSearch,"'% ")
                 )
                   })

                    table()

              })




       session$onSessionEnded(function() { dbDisconnect(con) })
         })


ui_panel <- 
        tabPanel("Text Input Test",
          sidebarLayout(
            sidebarPanel( 

             textInput("textSearch", " Search for keyword", ''),


                       br(),
                       submitButton("Update Table View"),
                       br()
           ),
           mainPanel(
           tabsetPanel(tabPanel("Table",tableOutput("tableview"))

           )
         )
    ))


 ui <- shinyUI(navbarPage(" ",ui_panel))

runApp(list(ui=ui,server=server))

Similar questions How to read a TextInput in ui.R, process a query with this value in global.R and show in server.R using Shiny which deals with output and dealing with a global file which I do not have in my case. 类似的问题如何使用ui.R读取TextInput,使用global.R中的该值处理查询,并使用Shiny在server.R中显示,该方法处理输出并处理我本人没有的全局文件。

Any insight will be welcome. 任何见识将受到欢迎。

Your current code should construct this SQL: 您当前的代码应构造此SQL:

SELECT ... FROM publications WHERE publications.abstract LIKE %'test'%

The issue: % should be inside '' . 问题: %应该在''

Updated line: 更新的行:

paste0("SELECT author, title, publicationDate,  FROM publications WHERE publications.abstract LIKE '%",input$textSearch,"%' ")

Then it should construct this SQL: 然后,应构造此SQL:

SELECT author, title, publicationDate, FROM publications WHERE publications.abstract LIKE '%test%'

For testing, I would recommend to render the generated SQL as text with renderText() and verbatimTextOutput() and see the outcome. 为了进行测试,我建议使用renderText()verbatimTextOutput()将生成的SQL渲染为文本,然后查看结果。

You can also consider to use glue package to construct SQL queries safely. 您也可以考虑使用胶水包安全地构造SQL查询。

For your second question, Action Buttons article might be useful. 对于第二个问题, “操作按钮”文章可能会有用。

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

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