GOLNG using the Postgres database

Source: Internet
Author: User
Tags sprintf postgres database
This is a creation in Article, where the information may have evolved or changed. "' Gopackage mainimport (" FMT "" Log "" Time "" Github.com/go-xorm/xorm "_" GITHUB.COM/LIB/PQ ") var engine *xorm. Enginefunc Main () {}func init () {psqlinfo: = FMT. Sprintf ("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", "127.0.0.1", 5432, "Postgres", "12345678", " Postgres ") var err errorengine, err = Xorm. Newengine ("Postgres", psqlinfo) if err! = Nil {fmt. PRINTLN (Err)}engine. Showsql ()//Show SQL statement err = engine. Ping () if err! = Nil {fmt. PRINTLN (Err)}}func addaccounttest () {account: = new account account. Balance = 100.99account. Version = 1account. Name = "Arvin" account = account. Create () fmt. Println (account. ID)}type account struct {ID Int64 ' JSON: "id" ' name ' String ' xorm: "Name VARCHAR (255)" JSON: "Name" ' Balance float64 ' xorm: "Bal ance NOT NULL DOUBLE "JSON:" balance "' version int ' xorm:" version default 1 int (one) "JSON:" Version "' Created time. Time ' Xorm: "Created NOT NULL DATETIME" JSON: "Created" ' Updated time. Time ' Xorm: "Updated NOT NULL DATETIME" JSON: "Updated" '}func (Self *account) TableName () string {return "accounts"}//create data table func (self *account) createtable () error {err: = engine. Createtables (self) if err! = Nil {log. Fatalln (Err)}return err}func (self *account) Create () *account {_, Err: = engine. Insertone (self) if err! = Nil {return Nil}return self}//by ID get func Getarticlebyid (ID Int64) *account {item: = new (account)/ /has, err: = engine. Where ("id =?", id). Get (item) has, _: = engine. ID (ID). Get (item) if!has {return Nil}return item}//allfunc selectall () []*account {var ACCs []*accountengine.sql (FMT. Sprintf ("Select * from%s", new.) TableName ())). Find (&ACCS) return accs}//uses SQL to remove the func deleteuserbysql (name string) bool {result, err: = engine. Exec (FMT. Sprintf ("Delete from%s where name = '%s '", new.) TableName (), name)) If Err! = Nil {log. PRINTLN (ERR) return false}rows, err: = result. Rowsaffected () If err = = Nil && rows > 0 {fmt. Println ("Rows:", rows) return True}return false}func (self *account) Update () {engine. Id (self. ID). Update(self)} "457 reads  
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.