Beego operational database

Beego embedded in the ORM framework,

1.

① may structure data table associating, only by the structure and the object will be able to operate the database.

② The generated database table structure.

the init FUNC () { 
	// set up basic information database 
	orm.RegisterDataBase ( "default", "MySQL", "root:? @ ADMIN tcp (127.0.0.1:3306) / the Test charset = utf8") 
	// data mapping model 
	orm .RegisterModel (new new (the User)) 
	// Make table 
	orm.RunSyncdb ( "default", to false, to true) 
}

2.ORM object operation database

① Insert

		// 1 has ORM objects 
		O: orm.NewOrm = () 
		// 2 has a data to be inserted in the object structure. 
		User: models.User = {} 
		// Assignment. 3 for a structure subject 
		user.Name = " 111 " 
		user.Pwd =" 222 " 
		//. 4 insert. 
		_, ERR: = o.Insert (User &) 
		! IF ERR = nil { 
			beego.Info (" insert failed ", ERR) 
			return 
		}

② query

		// 1 has ORM objects 
		O: orm.NewOrm = () 
		// 2 object query. 
		User: models.User = {} 
		// object field value specify a query. 3 
		user.name = "111" 
		user.id = 3 
		. 4 // query - Id parameter can be omitted only 
		@ ERR: = o.Read (& User) 
		ERR: = o.Read (& User, "the Name", "Id") 
		IF ERR = nil {! 
			beego.Info ( "query failed", ERR) 
			return 
		}

③ update

	// 1 has ORM objects 
	O: orm.NewOrm = () 
	// 2 queries the object structure being updated 
	User: models.User = {} 
	// need to query the data update. 3 
	user.id = 1 
	ERR: o.Read = (& User) 
	. reassigned to the data. 4 // 
	IF ERR == {nil 
		user.name = "999" 

		// update. 5. 
		NUM, ERR: = o.Update (User &) 
		! IF ERR = {nil 
			beego.Info ( "update failed", ERR) 
			return 
		} 
		beego.Info ( "the update is successful, a total update:", num, "strips") 
	}

④ Delete

	// 1 has orm objects 
	O: orm.NewOrm = () 
	// 2 deleted objects. 
	User: models.User = {} 
	// Specify that an object is deleted. 3 
	//user.Id = 2 
	// NUM, err: = o.Delete (& user) // Id conditions only, you do not add parameters 
	user.Pwd = "222" 
	// 4 deleted. 
	NUM, ERR: = o.Delete (& the User, "Pwd") 
	IF ERR =! {nil 
		beego.Info ( "delete failed", ERR) 
		return 
	} 
	beego.Info ( "deleted successfully, a total deleted:", num, "strips")

the above.

Guess you like

Origin www.cnblogs.com/jianyingjie/p/11774328.html