Always do not understand when using the go language, why also to install the Beego, think that using go can solve all the problems, the results of the friend's nudge, just realize:
Go and Beego relationship is like the relationship between Nodejs and THINKJS, so there is no doubt why continue to install Beego to complete the project run, Beego is actually a fast development Go application HTTP framework , go Language technology Daniel, can be used to quickly develop APIs, WEB, backend services and other applications, is a restful framework. Attach Beego's official website again (http://beego.me/docs/intro/)
Then start our database "Delete and change" Operation!
The 1.golang itself does not provide a driver to connect to MySQL, but it defines a standard interface for third-party development drivers. Here to connect MySQL can use third-party libraries, third-party libraries recommend the use of Https://github.com/Go-SQL-Driver/MySQL this driver , update maintenance is better; To download the driver, execute it in cmd
Go get Github.com/go-sql-driver/mysql
2. Create a test database (Navicat We've been using, use it as an example)
CREATE DATABASE Golang; CREATE TABLE IF not EXISTS ' Golang '. ' User ' (' userid ' INT (one) UNSIGNED not NULL auto_increment COMMENT ' subscriber number ', ' u Sername ' VARCHAR' not null COMMENT ' user name ', ' Userage ' TINYINT (3) UNSIGNED not null DEFAULT 0 COMMENT ' user age ', ' Usersex ' TINYINT (3) UNSIGNED not NULL DEFAULT 0 COMMENT ' user gender '== 1== = ' User table '
3. Connect the database: The database connection is connected using the Datebase/sql open function
DB, err: = SQL. Open ("MySQL", "User:[email Protected] (localhost:5555)/dbname?charset=utf8") where the connection parameters can be in the following forms: [email protected] ( /path/to/socket)/dbname?charset=utf8user:[email protected] (localhost:5555)/dbname?charset= Utf8user:[email Protected]/dbnameuser:[email protected] ([de:ad:be:ef::ca:fe]:80)/dbname
Generally, we all use the second kind.
4. Write the program in Liteide, complete with the following code:
Package Mainimport (_"Crud/routers" "Database/sql" "FMT" _ "Github.com/go-sql-driver/mysql") Func main () {Query()}//Add data func Insert () {db, err:= SQL. Open ("MySQL", "Root:[email Protected]/golang?charset=utf8") Checkerr (err) stmt, err:= db. Prepare (' INSERT user (Userid,username,userage,usersex) VALUES (?,?,?,?) ') Checkerr (ERR) res, err:= stmt. Exec (1, "Mary", 20, 1) Checkerr (err) ID, err:=Res. Lastinsertid () Checkerr (err) fmt. Println (ID)}//Delete data func remove () {db, err:= SQL. Open ("MySQL", "Root:[email Protected]/golang?charset=utf8") Checkerr (err) stmt, err:= db. Prepare (' DELETE from user WHERE userid=?') Checkerr (ERR) res, err:= stmt. Exec (1) Checkerr (err) num, err:=Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (num)}//Update Data func update () {db, err:= SQL. Open ("MySQL", "Root:[email Protected]/golang?charset=utf8") Checkerr (err) stmt, err:= db. Prepare (' UPDATE user SET userage=?,usersex=? WHERE userid=?') Checkerr (ERR) res, err:= stmt. Exec (21, 2, 2) Checkerr (err) num, err:=Res. Rowsaffected () Checkerr (err) fmt. PRINTLN (num)}//Query data funcQuery() {db, err:= SQL. Open ("MySQL", "Root:[email Protected]/golang?charset=utf8") Checkerr (ERR) rows, err:= db. Query ("SELECT * from User") Checkerr (err)// //Normal demo for rows. Next () {var useridintvar username string var userageintvar usersexintrows. Columns () Err= Rows. Scan (&userid, &username, &userage, &usersex) Checkerr (err) fmt. PRINTLN (userid) fmt. PRINTLN (username) fmt. Println (userage) fmt. Println (usersex)}}func Checkerr (err error) {ifErr! =Nil {panic (err)}}
So in this table for the increase and deletion check ...
I didn't use the ORM framework in Beego because I just started to ignore the ORM when I was watching .... A little negligent,,, to study again tomorrow.
Go--beego database additions and deletions change