首页 > 代码库 > go 读取数据库所有表数据显示到网页(便于测试时看)

go 读取数据库所有表数据显示到网页(便于测试时看)

技术分享
  1 package Network
  2 
  3 import (
  4     "SQL/myDB"
  5     "database/sql"
  6     "logs"
  7     "net/http"
  8 )
  9 
 10 func dbPage(w http.ResponseWriter, req *http.Request) {
 11     printRequestInfo(req)
 12     w.Header().Set("Content-Type", "text/html;charset=utf-8")
 13     w.Write([]byte(`
 14 <!DOCTYPE html>
 15 <html>
 16 <head>
 17 <title>数据库所有数据</title>
 18 </head>
 19 <body>
 20     `))
 21     w.Write([]byte(`
 22     <a href="http://www.mamicode.com/setData?page=resetDB"><h1>重置数据</h1></a>
 23     <a href="http://www.mamicode.com/setData?page=copyDB"><h1>复制数据</h1></a>
 24     `))
 25     defer w.Write([]byte(`
 26 </body>
 27 </html>
 28     `))
 29     w.Write([]byte(`
 30     <h1>以下是数据库每个表前一百条,方便你们接口联调</h1>
 31     `))
 32     db, err := myDB.GetDB()
 33     if err != nil {
 34         w.Write([]byte("<p>" + err.Error() + "</p>"))
 35         return
 36     }
 37 
 38     description := []string{
 39         `1.objects(id int64 pk,nickname string,iconURL string,isUser bool,custom jsonb)
 40         `, `2.users(id int64 fk pk,account string unique,password string,isManager bool)
 41         `, `3.groups(id int64 fk pk,createtime time)
 42         `, `4.members(groupID int64 fk,userID int64 fk,pk(groupID,userID))
 43         `, `5.messages(id int64 pk,content string,sender int64 fk,receiver int64 fk,createTime time)
 44         `, `6.sessions(senderID int64 fk,receiverID  int64 fk,lastMsgID int64 fk,pk(senderID,receiverID));
 45         `,
 46     }
 47     Sql := []string{
 48         `select * from objects
 49         `, `select * from users
 50         `, `select * from groups
 51         `, `select * from members
 52         `, `select * from messages
 53         `, `select * from sessions
 54         `,
 55     }
 56     for idx := 0; idx < len(Sql); idx++ {
 57         w.Write([]byte("<h2>" + description[idx] + "</h2>"))
 58         err := showData(w, db, Sql[idx])
 59         if err != nil {
 60             logs.Print("dbPage error:", err.Error())
 61             return
 62         }
 63     }
 64 }
 65 
 66 func showData(w http.ResponseWriter, db *sql.DB, Sql string) error {
 67     Sql += " limit 100"
 68     rows, err := db.Query(Sql)
 69     if err != nil {
 70         w.Write([]byte("<p>" + err.Error() + "</p>"))
 71         return err
 72     }
 73     defer rows.Close()
 74 
 75     w.Write([]byte(`
 76         <table border="1">
 77             <tr>
 78             `))
 79     defer w.Write([]byte(` 
 80             </tr>
 81         </table> `))
 82 
 83     cols, err := rows.Columns()
 84     if err != nil {
 85         w.Write([]byte(err.Error()))
 86         return err
 87     }
 88     for _, val := range cols {
 89         w.Write([]byte("<th>" + val + "</th>"))
 90     }
 91 
 92     return htmlOutputRows(w, len(cols), rows)
 93 }
 94 func htmlOutputRows(w http.ResponseWriter, colNum int, rows *sql.Rows) error {
 95     str := make([][]byte, colNum)
 96     for rows.Next() {
 97         w.Write([]byte(` <tr>`))
 98         args := make([]interface{}, 0)
 99         for idx := 0; idx < colNum; idx++ {
100             args = append(args, &str[idx])
101         }
102         err := rows.Scan(args...)
103         if err != nil {
104             w.Write([]byte(err.Error()))
105             return err
106         }
107         for _, val := range str {
108             w.Write([]byte("<td>" + string(val) + "</td>"))
109         }
110         w.Write([]byte("</tr>"))
111     }
112     return nil
113 }
/dbPage

 

go 读取数据库所有表数据显示到网页(便于测试时看)