Exemplo n.º 1
0
func Connect(info map[string]interface{}) (conn *Connection, err error) {
	defer handleError(&err)

	host := mapCString(info, "host")
	defer cfree(host)
	port := mapint(info, "port")
	uname := mapCString(info, "uname")
	defer cfree(uname)
	pass := mapCString(info, "pass")
	defer cfree(pass)
	dbname := mapCString(info, "dbname")
	defer cfree(dbname)
	unix_socket := mapCString(info, "unix_socket")
	defer cfree(unix_socket)
	charset := mapCString(info, "charset")
	defer cfree(charset)

	conn = &Connection{}
	conn.handle = C.mysql_init(nil)
	if C.mysql_real_connect(conn.handle, host, uname, pass, dbname, C.uint(port), unix_socket, 0) == nil {
		defer conn.Close()
		return nil, conn.lastError(nil)
	}

	if C.mysql_set_character_set(conn.handle, charset) != 0 {
		defer conn.Close()
		return nil, conn.lastError(nil)
	}
	return conn, nil
}
Exemplo n.º 2
0
//设置本连接的字符集, 成功返回true
func (mysql *MySQL) SetCharacterSet(cs string) bool {
	s := C.CString(cs)
	defer C.free(unsafe.Pointer(s))

	f := C.mysql_set_character_set(mysql.my, s)
	mysql.errno = 5
	if f == 0 {
		mysql.errno = 0
		return true
	}
	return false
}