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 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错