标签:turn 数据库查询 host uid for tcp user 返回 utf8
刚开始接触Golang,只是随笔记下几个方法,原理还不是很清晰,暂时只是能实现的地步,随笔几点吧
1.数据库连接
var { dbhostip = "" dbhostport = "" dbhostuser = "" dbhostpassword = "" } //返回db连接 func DbOpen() (db_obj *sql.DB, err error){ db_obj,err_open := sql.Open("mysql",dbhostuser+":"+dbhostpassword+"@tcp("+dbhostip+":"+dbhostport+")/数据库名?charset=utf8") if err_open != nil { }else{ } return }
2.数据库查询
//数据库单条查询 func DbQueryRow(db *sql.DB, str string) (result string, err error){ row_err := db.QueryRow(str).Scan(&result) if row_err != nil (){ }else{ } return } //数据库一般查询 //数据库单条查询 func DbQueryRow(db *sql.DB, str string) (rows *sql.Rows, err error){ rows ,row_err := db.Query(str) if row_err != nil (){ }else{ } return }
//对rows处理for rows.Next() {
var uid int
var name, password stringrows.Scan(&uid, &name, &password)
fmt.Println("uid:", uid, "name:", name, "password:", password)
}
标签:turn 数据库查询 host uid for tcp user 返回 utf8
原文地址:http://www.cnblogs.com/xiledada/p/7417170.html