GOLNG using the Postgres database
Last Update:2018-01-19
Source: Internet
Author: User
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