亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Go
滄海一幻覺 2021-09-13 16:37:31
我正在嘗試使用 GO 作為后端將 Sql 查詢的結果嵌入到 html 表中。為了在 Go 中迭代行結果,使用了 Rows.Next() 函數。這適用于打印到控制臺窗口,但不適用于 html 表。這是我的 Go 代碼:package main// Database connection Code for http://play.golang.org/p/njPBsg0JjDimport (    "net/http"    "html/template"    "fmt"    "github.com/LukeMauldin/lodbc"    "github.com/jmoiron/sqlx"    "database/sql")//declare database classvar db *sqlx.DBtype 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}} ,但這沒有用任何投入將不勝感激
查看完整描述

2 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

我使用這個變體:



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

    type User struct {

        Name1  string

        Name2  string

    }


    rows, err := database.Query("select  .......;")

    if err != nil {

        log.Println(err)

    }

    defer rows.Close()

    user_current := []User{}

    for rows.Next() {

        p := User{}

        err := rows.Scan(&p.Name1, &p.Name2 )

        if err != nil {

            fmt.Println(err)

            continue

        }

        user_current = append(user_current, p)

    }

    tmpl, _ := template.ParseFiles("main_page.html")

    tmpl.Execute(w, user_current)

}

html


<table>

     <thead><th>name1/th><th>name2</th></thead>

            {{range . }}

            <tr>

                <td>{{.Name1}}</td>

                <td>{{.Name2}}</td>

            </tr>

            {{end}}

        </table>


查看完整回答
反對 回復 2021-09-13
  • 2 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號