在golang中显示数据库的结果

huangapple go评论116阅读模式
英文:

Displaying results from a database in golang

问题

我正在从一个表单中读取一个MySQL查询输入:

  1. <h1>MySQL页面</h1>
  2. <small>在这里执行查询和编辑数据库</small>
  3. <form method="get" action="">
  4. <label for="sqlQuery">MySQL查询:</label>
  5. <input type="text" id="sqlQuery" name="sqlQuery">
  6. <button type="submit">执行查询</button>
  7. </form>

之后,我想使用GoLang在同一页上显示结果,但它一直告诉我:

  1. # command-line-arguments
  2. ./sql.go:128: 无法将结果(类型为sql.Result)转换为字符串类型

请记住,这是我写的第一个GoLang应用程序,如果这是一个简单的问题,我很抱歉,以下是GoLang代码:

  1. func sqlQueryHandler(response http.ResponseWriter, request *http.Request){
  2. userName := getUserName(request)
  3. db, err := sql.Open("mysql", userName)
  4. fmt.Fprintf(response, sqlPage)
  5. sqlCommand := request.FormValue("sqlQuery")
  6. //fmt.Fprintf(response, sqlCommand)
  7. if err != nil {
  8. fmt.Fprintf(response, "\n\n在执行MySQL命令时发生错误:%s", err)
  9. panic(err)
  10. } else {
  11. data, err := db.Exec(sqlCommand)
  12. if err != nil {
  13. http.Redirect(response, request, "/error", 302)
  14. } else {
  15. // 在这里显示SQL查询的输出
  16. }
  17. }
  18. }
英文:

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

  1. &lt;h1&gt;MySQL Page&lt;/h1&gt;
  2. &lt;small&gt;Perform queries and edit the database from here&lt;/small&gt;
  3. &lt;form method=&quot;get&quot; action=&quot;&quot;&gt;
  4. &lt;label for=&quot;sqlQuery&quot;&gt;MySQL Query:&lt;/label&gt;
  5. &lt;input type=&quot;text&quot; id=&quot;sqlQuery&quot; name=&quot;sqlQuery&quot;&gt;
  6. &lt;button type=&quot;submit&quot;&gt;Perform Query&lt;/button&gt;
  7. &lt;/form&gt;

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

  1. # command-line-arguments
  2. ./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:

  1. func sqlQueryHandler(response http.ResponseWriter, request *http.Request){
  2. userName := getUserName(request)
  3. db, err := sql.Open(&quot;mysql&quot;, userName)
  4. fmt.Fprintf(response, sqlPage)
  5. sqlCommand := request.FormValue(&quot;sqlQuery&quot;)
  6. //fmt.Fprintf(response, sqlCommand)
  7. if err != nil {
  8. fmt.Fprintf(response, &quot;\n\nAn error occured during your MySQL command: %s&quot;, err)
  9. panic(err)
  10. } else {
  11. data, err := db.Exec(sqlCommand)
  12. if err != nil {
  13. http.Redirect(response, request, &quot;/error&quot;, 302)
  14. } else {
  15. // display the output of the sql query here
  16. }
  17. }
  18. }

答案1

得分: 2

根据你的代码,这里有一个示例:

  1. func sqlQueryHandler(response http.ResponseWriter, request *http.Request) {
  2. var (
  3. userName = getUserName(request)
  4. sqlCommand = request.FormValue("sqlQuery")
  5. )
  6. db, err := sql.Open("mysql", userName)
  7. if err != nil {
  8. fmt.Fprintf(response, "\n\n在执行MySQL命令时发生错误:%s", err)
  9. // 如果发生错误,你可以在这里停止执行,不需要else语句
  10. panic(err)
  11. }
  12. rows, err := db.Query(sqlCommand)
  13. if err != nil {
  14. http.Redirect(response, request, "/error", 302)
  15. // 返回,所以不需要else语句
  16. return
  17. }
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer rows.Close()
  22. for rows.Next() {
  23. var (
  24. name string
  25. age int
  26. )
  27. if err := rows.Scan(&name, &age); err != nil {
  28. panic(err)
  29. }
  30. fmt.Printf("%s is %d\n", name, age)
  31. }
  32. if err := rows.Err(); err != nil {
  33. panic(err)
  34. }
  35. }

然而,这种方法存在几个问题:

  • 你从服务器外部传递了SQL语句。任何访问这个函数的人都可以读取你服务器上的所有数据。
  • Go语言的一个优点是它是一种强类型语言。但是在这里,你正在构建一个处理通用SQL查询的函数,这与“强类型语言”的范例相矛盾。你可以编写处理不同结构化数据的通用函数(比如json.Unmarshal()),但是特别是在编程Go语言的早期阶段,你不应该这样做。
英文:

Here an example based on your code:

  1. func sqlQueryHandler(response http.ResponseWriter, request *http.Request) {
  2. var (
  3. userName = getUserName(request)
  4. sqlCommand = request.FormValue(&quot;sqlQuery&quot;)
  5. )
  6. db, err := sql.Open(&quot;mysql&quot;, userName)
  7. if err != nil {
  8. fmt.Fprintf(response, &quot;\n\nAn error occured during your MySQL command: %s&quot;, err)
  9. // if you panic you stop here anyway. no else needed
  10. panic(err)
  11. }
  12. rows, err := db.Query(sqlCommand)
  13. if err != nil {
  14. http.Redirect(response, request, &quot;/error&quot;, 302)
  15. // return, so no else is needed
  16. return
  17. }
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer rows.Close()
  22. for rows.Next() {
  23. var (
  24. name string
  25. age int
  26. )
  27. if err := rows.Scan(&amp;name, &amp;age); err != nil {
  28. panic(err)
  29. }
  30. fmt.Printf(&quot;%s is %d\n&quot;, name, age)
  31. }
  32. if err := rows.Err(); err != nil {
  33. panic(err)
  34. }
  35. }

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.

huangapple
  • 本文由 发表于 2017年8月18日 00:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/45740042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定