Go read database All table data display to Web page (easy to see when testing)

Source: Internet
Author: User
Tags sessions

1  PackageNetwork2 3 Import (4"Sql/mydb"5"Database/sql"6"Logs"7"Net/http"8 )9 TenFunc dbpage (w http. Responsewriter, req *http. Request) { One Printrequestinfo (req) AW.header (). Set ("Content-type", "Text/html;charset=utf-8") -W.write ([]byte(` -<! DOCTYPE html> the - -<title> Database All data </title> - +<body> -     `)) +W.write ([]byte(` A<a href= "\setdata?page=resetdb" > at<a href= "\setdata?page=copydb" > -     `)) -Defer W.write ([]byte(` -</body> - -     `)) inW.write ([]byte(` - to     `)) +DB, Err: =Mydb.getdb () -     ifErr! =Nil { theW.write ([]byte("<p>" + Err.) Error () + "</p>")) *         return $     }Panax Notoginseng  -Description: =[]string{ the' 1. Objects (ID Int64 pk,nickname string,iconurl string,isuser bool,custom jsonb) +', ' 2. Users (id int64 FK Pk,account string Unique,password string,ismanager bool) A', ' 3. Groups (ID int64 FK Pk,createtime time) the', ' 4. Members (GroupID Int64 fk,userid Int64 fk,pk (Groupid,userid)) +', ' 5. Messages (id Int64 pk,content string,sender int64 fk,receiver Int64 fk,createtime time) -', ' 6. Sessions (SenderID Int64 fk,receiverid Int64 fk,lastmsgid Int64 fk,pk (Senderid,receiverid)); $         `, $     } -SQL: =[]string{ -' SELECT *From objects the', ' SELECT *From users -', ' SELECT *From groupsWuyi', ' SELECT * from members the', ' SELECT *From messages -', ' SELECT *From sessions Wu         `, -     } About      forIDX: = 0; IDX < Len (SQL); idx++ { $W.write ([]byte(")) -ERR: =ShowData (w, DB, Sql[idx]) -         ifErr! =Nil { -Logs. Print ("Dbpage Error:", Err. Error ()) A             return +         } the     } - } $  theFunc showData (w http. Responsewriter, DB *SQL. DB, SQL String) error { theSQL + = "Limit 100" theRows, Err: =db. Query (SQL) the     ifErr! =Nil { -W.write ([]byte("<p>" + Err.) Error () + "</p>")) in         returnErr the     } the defer rows. Close () About  theW.write ([]byte(` the<table border= "1" > the<tr> +             `)) -Defer W.write ([]byte(`  the</tr>Bayi</table> `)) the  thecols, err: =rows. Columns () -     ifErr! =Nil { -W.write ([]byte(Err. Error ())) the         returnErr the     } the      for_, Val: =range Cols { theW.write ([]byte("<th>" + val + "</th>")) -     } the  the     returnHtmloutputrows (W, Len (cols), rows) the }94Func htmloutputrows (w http. Responsewriter, Colnumint, Rows *SQL. Rows) Error { thestr: = make ([]byte, Colnum) the      forrows. Next () { theW.write ([]byte(' <tr>`))98Args: = make ([]Interface{}, 0) About          forIDX: = 0; IDX < Colnum; idx++ { -args = append (args, &Str[idx])101         }102ERR: =rows. Scan (args ...)103         ifErr! =Nil {104W.write ([]byte(Err. Error ())) the             returnErr106         }107          for_, Val: =Range STR {108W.write ([]byte("<td>" + String (val) + "</td>"))109         } theW.write ([]byte("</tr>"))111     } the     returnNil113}
/dbpage

Go read database All table data display to Web page (easy to see when testing)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.