Just beginning to contact Golang, just write down a few methods, the principle is not very clear, temporarily just can achieve the point, essay points
1. Database connection
var { = "" = "" = "" = ""}//Return DB connection Func dbopen () (Db_obj *sql. DB, err error) { db_obj, Err_open: = sql. Open ("MySQL", dbhostuser+ ":" +dbhostpassword+ "@tcp (" +dbhostip+ ":" +dbhostport+ ")/database name? Charset=utf8") if err_open ! = nil { }else{ } return }
2. Database queries
//Database Single QueryFunc dbqueryrow (db *sql. DB, strstring) (Resultstring,err Error) {Row_err: = db. Queryrow (str). Scan (&result)ifRow_err! =nil () {}Else{ } return}//Database General query//Database single queryFunc dbqueryrow (db *sql. DB, strstring) (Rows *sql. Rows,err Error) {Rows, Row_err: = db.Query (str)ifRow_err! =nil () {}Else{}return}
To Rows Processing
for rows.Next() {
varUID int
varname, password string
rows.Scan(&uid, &name, &password)
fmt.Println("uid:", uid, "name:", name, "password:", password)
}
Golang Database Operations