Go+postgresql Server

Source: Internet
Author: User
Tags stmt

1Func addgroup (w http. Responsewriter, req*http. Request) {2 3 outputreq (req)4 5Responsejson:=Map[string]interface{}{jsonkeymessage: "", Jsonkeyresult:nil}6 7 Initresponse (W)8Err:=req. Parseform ()9     ifErr!=Nil {Ten Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) One         return A     } -  -Nickname:=ToString (req. Form["nickname"]) the     Delete(req. Form, "nickname") -Icon:=ToString (req. Form["icon"]) -     Delete(req. Form, "icon") -  +Requestjson:=Initrequestjson (req. Form) -Custominfo, err:=JSON. Marshal (Requestjson) +     ifErr!=Nil { A Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) at         return -     } -  -DB, Err:=Opensql () -Defer db.Close() -     ifErr!=Nil { in Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) -         return to     } +  -TX, ERR:=Db.Begin() the     ifErr!=Nil { * Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) $         returnPanax Notoginseng     } -  thestmt, err:=Tx.Prepare(` +     Insert  intoobjects (Nickname,icon,custom,isuser) A     Values($1,$2,$3, false) returning ID the     `) +     ifErr!=Nil { - Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror) $         return $     } -  -Rows, err:=stmt. Query (nickname, Icon, Custominfo) the     ifErr!=Nil { -Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback())Wuyi writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the         return -     } Wu  -     if!rows.Next() { AboutErrstring:="Add Groupfailed! "+ErrorString (TX.Rollback()) $ writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) -         return -     } -  A     varGroupID Int64 +Err=Rows. Scan (&GroupID) the     ///Fmt. Println ("GroupID:",+GroupID) -     ifErr!=Nil { $Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) the writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the         return the     } the  -Err=Rows.Close() in     ifErr!=Nil { theErrstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) the writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) About         return the     } theErr=stmt.Close() the     ifErr!=Nil { +Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) - writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the         returnBayi     } thestmt, err=Tx.Prepare("Insert  intoGroups (Id,createtime)Values($1, now ()) ") theDefer stmt.Close() -     ifErr!=Nil { -Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback()) the writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the         return the     } the  -result, err:=stmt.Exec(GroupID) the     //ROWS1, err:=Tx.Exec("Insert  intoGroups (Id,createtime)Values('"+ FMT. Sprint (GroupID) + "', now ()) ") the     ifErr!=Nil { theErrstring:=Err. Error ()+" "+ErrorString (TX.Rollback())94 writeresponse (errstring, Responsejson, W, http. Statusinternalservererror) the         return the     } the 98Affect, err:=result. Rowsaffected () About     ifErr!=Nil { -Errstring:=Err. Error ()+" "+ErrorString (TX.Rollback())101 writeresponse (errstring, Responsejson, W, http. Statusinternalservererror)102         return103     }104     ifAffect!= 1 { theErrstring:="Add Groupfailed! "+ErrorString (TX.Rollback())106 writeresponse (errstring, Responsejson, W, http. Statusinternalservererror)107         return108     }109  the_, Err=Tx.Exec("Commit")111     ifErr!=Nil { the Writeresponse (Err. Error (), Responsejson, W, http. Statusinternalservererror)113         return the     } theResponsejson[Jsonkeyresult] =FMT. Sprint (GroupID) the Writeresponse ("Success", Responsejson, W, http. Statusok)117}
PostgreSQL Transaction

Go+postgresql Server

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.