The use of the Beego framework ORM for Go Language (ii)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Package Main
Import (
"FMT"
"Github.com/astaxie/beego/orm"
_ "Github.com/go-sql-driver/mysql"
)
Type User struct {
Id         int ' orm: ' Auto '
Name       String
profile_id int
}
Func init () {
Orm. Registermodel (New User)
Orm. RegisterDatabase ("Default", "MySQL", "Ta3:ta3@/ta3?charset=utf8")
Orm. RUNSYNCDB ("Default", False, True)
}
Func Main () {
VAR (
sSQL string
Err  Error
)
var users []user
Orm. Debug = TRUE//Open query log
o: = orm. Neworm ()
O.using ("Default")//default is used, you can specify as a different database
Query for large data volume do not know how performance?
sSQL = "Select Id, Name from User"
num, err: = O.raw (sSQL). QueryRows (&users)
If Err = = Nil && num > 0 {
For I, _: = Range Users {
Fmt. Println (Users[i]. Name)
}
}
Perform the update
sSQL = "Update User set name= ' AAA ' where id= 1"
_, Err = O.raw (sSQL). Exec ()
If err! = Nil {
Fmt. PRINTLN ("Update Error")
}
Transaction
Err = O.begin ()
sSQL = "INSERT into User (Id, Name, profile_id) VALUES (3, ' bbbb ', 9)"
_, Err = O.raw (sSQL). Exec ()
If err! = Nil {
O.rollback ()
} else {
O.commit ()
}
The following usage can be adapted to all SQL notation
sSQL = FMT. Sprintf ("INSERT into User (Id, Name, profile_id) VALUES (4, ' bbbb ',%d)", 10)
_, Err = O.raw (sSQL). Exec ()
If err! = Nil {
Fmt. PRINTLN ("Insert 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.