猿问

如何使用 GO (golang) 在 HTML 中嵌入 SQLX 结果

我正在尝试使用 GO 作为后端将 Sql 查询的结果嵌入到 html 表中。为了在 Go 中迭代行结果,使用了 Rows.Next() 函数。这适用于打印到控制台窗口,但不适用于 html 表。这是我的 Go 代码:


package main

// Database connection Code for http://play.golang.org/p/njPBsg0JjD


import (

    "net/http"

    "html/template"

    "fmt"

    "github.com/LukeMauldin/lodbc"

    "github.com/jmoiron/sqlx"

    "database/sql"

)


//declare database class

var db *sqlx.DB


type webdata struct {

    Title string

    Heading string

    GridTitle string

    ColumnHeading [9]string

    RowData [9]string

    NumOfRows *sql.Rows

}


//this is the function handler to handle '/mbconsole'

func ConsoleHandler(w http.ResponseWriter, r *http.Request) {


    //declare an instance of webdata

    var wdata webdata


    //connect to database

    //Set ODBC driver level

    lodbc.SetODBCVersion(lodbc.ODBCVersion_3)


    var err error

    //connect to a Microsoft SQL Server

    db, err = sqlx.Open("lodbc", "[connectionstring]")

    if err == nil {

        fmt.Println("Connection successful")

    }else{

        fmt.Println("SQL Connection error", err)

    }


    // Execute the queries

    rows, err := db.Query("[Select ...]")

    if err != nil {

        panic(err.Error())

    }



    // Get column names

    columns, err := rows.Columns()

    if err != nil {

        panic(err.Error())

    }



    // Make a slice for the values

    values := make([]interface{}, len(columns))


    // rows.Scan wants '[]interface{}' as an argument, so we must copy the

    // references into such a slice

    // See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details

    scanArgs := make([]interface{}, len(values))


    for i := range values {

        scanArgs[i] = &values[i]

    }

    //fill table headings, the table returns 9 columns so I just hard coded it

    for i:=0;i<9;i++ {

        wdata.ColumnHeading[i] = columns[i]

    }

我正确连接到数据库并使用“fmt”包我可以正确打印。但我不知道如何循环遍历 html 页面中返回的行数。有没有办法在 html 中将 sql.Rows 转换为正确的类型或循环设置整数次。

附:我尝试在 html 中使用 {{ $index := 3}}...{end}} ,但这没有用

任何投入将不胜感激


沧海一幻觉
浏览 223回答 2
2回答

一只名叫tom的猫

我使用这个变体:走func MainPageHandler(w http.ResponseWriter, r *http.Request) {&nbsp;&nbsp; &nbsp; type User struct {&nbsp; &nbsp; &nbsp; &nbsp; Name1&nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; Name2&nbsp; string&nbsp; &nbsp; }&nbsp; &nbsp; rows, err := database.Query("select&nbsp; .......;")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Println(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer rows.Close()&nbsp; &nbsp; user_current := []User{}&nbsp; &nbsp; for rows.Next() {&nbsp; &nbsp; &nbsp; &nbsp; p := User{}&nbsp; &nbsp; &nbsp; &nbsp; err := rows.Scan(&p.Name1, &p.Name2 )&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; user_current = append(user_current, p)&nbsp; &nbsp; }&nbsp; &nbsp; tmpl, _ := template.ParseFiles("main_page.html")&nbsp; &nbsp; tmpl.Execute(w, user_current)}html<table>&nbsp; &nbsp; &nbsp;<thead><th>name1/th><th>name2</th></thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{range . }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{.Name1}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>{{.Name2}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{end}}&nbsp; &nbsp; &nbsp; &nbsp; </table>
随时随地看视频慕课网APP

相关分类

Go
我要回答