Go language query MySQL database

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Dalimport (     "Encoding/json"      "StrConv"       "Strings"      "Github.com/astaxie/beego/orm"     _ "github.com/ Go-sql-driver/mysql ") type Automethod struct {    actionid int ' orm:" Pk;auto;column (ActionId) "'         actionname string ' orm: ' Size (actionname) '     actionarg String ' orm: ' size (+); column (Actionarg) "'     actiontype int ' orm:" size (one); column ( ActionType) "'     methodrunname string ' orm:" size (n); column (Methodrunname) "'      assemblyid int ' orm: "size (one); column (Assemblyid)" '     classname string ' orm: "Siz The column (ClassName) "'     subactionid int ' orm:" size (one); column (Subactionid) "'      actionruntime string ' orm: ' Size, column (actionruntime) '}func init () {    orm. Registermodelwithprefix ("", New (Automethod))     orm. Registerdriver ("MySQL", Orm.) Drmysql)     orm. RegisterDatabase ("Default", "MySQL", "root:123456@tcp (10.101.42.64:3306)/mytest?charset=utf8")}func Printactionjson () interface{} {    o: = orm. Neworm ()     o.using ("MyTest")     var Actioninfos []*automethod     sel: = o.querytable ("Automethod")     count, err: = sel. All (&actioninfos)     if err! = Nil {        return "Number of Bars" + string (count) + Err. Error ()     }    //json convert     jsondata, err: = json. Marshal (Actioninfos)     if err! = Nil {        return Err. Error ()     }    newdata: = Make ([]automethod, Len (actioninFOS))     json. Unmarshal (Jsondata, &newdata)     return string (jsondata)}func Printactionmodel () interface {} {    o: = orm. Neworm ()     o.using ("MyTest")     var Actioninfos []*automethod     sel: = o.querytable ("Automethod")     count, err: = sel. All (&actioninfos)     if err! = Nil {        return "Number of Bars" + string (count) + Err. Error ()     }    //json convert     jsondata, err: = json. Marshal (Actioninfos)     if err! = Nil {        return Err. Error ()     }    newdata: = Make ([]automethod, Len (Actioninfos))      json. Unmarshal (Jsondata, &newdata)     return actioninfos}func PrintActionIdAndName () INTERFACe{} {    o: = orm. Neworm ()     o.using ("MyTest")     var actionids []int     var actionnames []string    raw: = O.raw ("Select Actionid,actionname from Automethod")     num, err: = Raw. QueryRows (&actionids, &actionnames)     var rowsdata = make ([]string, num, +)      if Err = = Nil {        for I: = Int64 (0); i < num; i++ {&nbsp ;            rowsdata[i] = StrConv. Itoa (Actionids[i]) + actionnames[i]        }         return strings. Join (Rowsdata, ",")     }    return err. Error ()}

 

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.