dongsuo0517 2017-08-17 16:04
浏览 104
已采纳

在Golang中显示来自数据库的结果

I'm reading a MySQL query input from a form:

<h1>MySQL Page</h1>
<small>Perform queries and edit the database from here</small>
<form method="get" action="">
  <label for="sqlQuery">MySQL Query:</label>
  <input type="text" id="sqlQuery" name="sqlQuery">
  <button type="submit">Perform Query</button>
</form>

After that I want to display the results on the same page using GoLang, however it keeps telling me that:

# command-line-arguments
./sql.go:128: cannot convert results (type sql.Result) to type string

Please keep in mind, this is the first golang app I've ever written so I apologize if this is a simple issue, here is the golang code:

func sqlQueryHandler(response http.ResponseWriter, request *http.Request){
  userName := getUserName(request)
  db, err := sql.Open("mysql", userName)
  fmt.Fprintf(response, sqlPage)
  sqlCommand := request.FormValue("sqlQuery")
  //fmt.Fprintf(response, sqlCommand)
  if err != nil {
    fmt.Fprintf(response, "

An error occured during your MySQL command: %s", err)
    panic(err)
  } else {
    data, err := db.Exec(sqlCommand)
    if err != nil {
      http.Redirect(response, request, "/error", 302)
    } else {
      // display the output of the sql query here
    }
  }
}
  • 写回答

1条回答 默认 最新

  • dph6308 2017-08-17 17:12
    关注

    Here an example based on your code:

    func sqlQueryHandler(response http.ResponseWriter, request *http.Request) {
        var (
            userName   = getUserName(request)
            sqlCommand = request.FormValue("sqlQuery")
        )
    
        db, err := sql.Open("mysql", userName)
        if err != nil {
            fmt.Fprintf(response, "
    
    An error occured during your MySQL command: %s", err)
            // if you panic you stop here anyway. no else needed
            panic(err)
        }
        rows, err := db.Query(sqlCommand)
        if err != nil {
            http.Redirect(response, request, "/error", 302)
            // return, so no else is needed
            return
        }
    
        if err != nil {
            panic(err)
        }
        defer rows.Close()
        for rows.Next() {
            var (
                name string
                age  int
            )
            if err := rows.Scan(&name, &age); err != nil {
                panic(err)
            }
            fmt.Printf("%s is %d
    ", name, age)
        }
        if err := rows.Err(); err != nil {
            panic(err)
        }
    }
    

    There are several problems however with this approach:

    • You are passing the sql from outside the server. Anyone accessing this can read all the data from your server.
    • One of Go's strengths is being a typed language. Here you are building a general sql query function which contradicts the typed language paradigm. You can write general function dealing with differently structured data (like json.Unmarshal()) -- but especially early in programming go you shouldn't.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Cesm如何关闭不用的模块呢
  • ¥15 vue2两层数据导出为excle
  • ¥15 有人能帮我做一下这个项目吗
  • ¥15 网络上传速率比下载速率高
  • ¥15 (标签-游戏|关键词-网页游戏)
  • ¥15 这个咋搞🥹可能需要手把手教
  • ¥15 RFID标签芯片ROCKY100配置成SPI主机问题
  • ¥15 接口和关键字的问题,多选
  • ¥15 yolov5s模型下载就卡住,没有运行结果
  • ¥20 androidstudio工具问题