当前位置: 首页>>代码示例>>Golang>>正文


Golang Rows.Columns方法代码示例

本文整理汇总了Golang中database/sql.Rows.Columns方法的典型用法代码示例。如果您正苦于以下问题:Golang Rows.Columns方法的具体用法?Golang Rows.Columns怎么用?Golang Rows.Columns使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在database/sql.Rows的用法示例。


在下文中一共展示了Rows.Columns方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: GetResult

func GetResult(rows *sql.Rows) ([]interface{}, []string) {
	if rows == nil {
		panic("rows is null")
	}
	columns, err := rows.Columns()

	if err != nil {
		panic(err.Error())
	}

	values := make([]interface{}, len(columns))
	scan_args := make([]interface{}, len(columns))
	for i := range values {
		scan_args[i] = &values[i]
	}

	var ret []interface{}
	for rows.Next() {
		err = rows.Scan(scan_args...)
		if err != nil {
			panic(err.Error())
		}

		row := ResultRow{}
		for i, col_name := range columns {
			row[col_name] = values[i]
		}
		ret = append(ret, row)
	}
	return ret, columns
}
开发者ID:JingxuanWang,项目名称:Playground,代码行数:31,代码来源:DA.go

示例2: rowsToStrings

func rowsToStrings(rows *sql.Rows) [][]string {
	cols, err := rows.Columns()
	if err != nil {
		panic(err)
	}
	pretty := [][]string{cols}
	results := make([]interface{}, len(cols))
	for i := range results {
		results[i] = new(interface{})
	}
	for rows.Next() {
		if err := rows.Scan(results[:]...); err != nil {
			panic(err)
		}
		cur := make([]string, len(cols))
		for i := range results {
			val := *results[i].(*interface{})
			var str string
			if val == nil {
				str = "NULL"
			} else {
				switch v := val.(type) {
				case []byte:
					str = string(v)
				default:
					str = fmt.Sprintf("%v", v)
				}
			}
			cur[i] = str
		}
		pretty = append(pretty, cur)
	}
	return pretty
}
开发者ID:liugangnhm,项目名称:cockroach,代码行数:34,代码来源:trace_test.go

示例3: readAll

func readAll(t *testing.T, rows *sql.Rows) resultSlice {
	defer rows.Close()

	cols, err := rows.Columns()
	if err != nil {
		t.Fatal(err)
	}

	colStrs := make([]*string, len(cols))
	for i := range cols {
		colStrs[i] = &cols[i]
	}

	results := resultSlice{colStrs}

	for rows.Next() {
		strs := make([]*string, len(cols))
		vals := make([]interface{}, len(cols))
		for i := range vals {
			vals[i] = &strs[i]
		}
		if err := rows.Scan(vals...); err != nil {
			t.Fatal(err)
		}
		results = append(results, strs)
	}

	if err := rows.Err(); err != nil {
		t.Fatal(err)
	}

	return results
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:33,代码来源:driver_test.go

示例4: sqlToMaps

func sqlToMaps(ctx *Ctx, sqlString string, sqlParams ...interface{}) (ret []map[string]interface{}, err error) {
	ret = []map[string]interface{}{}
	var rows *sql.Rows
	rows, err = ctx.db.Query(sqlString, sqlParams...)
	defer rows.Close()
	if err != nil {
		return
	}
	columns, err := rows.Columns()
	if err != nil {
		return
	}
	count := len(columns)
	values := make([]interface{}, count)
	valuePtrs := make([]interface{}, count)
	for i := 0; i < count; i++ {
		valuePtrs[i] = &values[i]
	}
	for rows.Next() {
		retRow := map[string]interface{}{}
		rows.Scan(valuePtrs...)
		for i, col := range columns {
			val := values[i]
			switch val.(type) {
			case []uint8:
				retRow[col] = string(val.([]uint8))
			default:
				retRow[col] = val
			}
		}
		ret = append(ret, retRow)
	}
	return
}
开发者ID:rogallic,项目名称:rogsit,代码行数:34,代码来源:fastFuncs.go

示例5: displayResult

func displayResult(rows *sql.Rows) {
	cols, cols_err := rows.Columns()

	if cols_err != nil {
		log.Fatalln(cols_err)
	}

	rawResult := make([][]byte, len(cols))
	result := make([]string, len(cols))

	dest := make([]interface{}, len(cols))
	for i, _ := range cols {
		dest[i] = &rawResult[i]
	}

	for rows.Next() {
		rows.Scan(dest...)

		for i, raw := range rawResult {
			result[i] = string(raw)
		}

		for j, v := range result {
			fmt.Printf("%s", v)
			if j != len(result)-1 {
				fmt.Printf(", ")
			}
		}
		fmt.Printf("\n")
	}
}
开发者ID:Gwillink,项目名称:textql,代码行数:31,代码来源:main.go

示例6: NewQueryResults

// Converts sql.Rows into QueryResults.
func NewQueryResults(rows *sql.Rows, rowLimit int) *QueryResults {
	columns, err := rows.Columns()
	if err != nil {
		return &QueryResults{Error: err}
	}
	data := make([][]string, 0)
	row := 1
	for rows.Next() && row < rowLimit {
		stringValues := make([]string, len(columns)+1)
		stringValues[0] = strconv.Itoa(row)
		pointers := make([]interface{}, len(columns))
		for i := 0; i < len(columns); i++ {
			pointers[i] = &stringValues[i+1]
		}
		if err := rows.Scan(pointers...); err != nil {
			return &QueryResults{Error: err}
		}
		data = append(data, stringValues)
		row += 1
	}
	return &QueryResults{
		Columns: append([]string{"Row"}, columns...),
		Data:    data,
	}
}
开发者ID:DevilEVG,项目名称:go-web-sql,代码行数:26,代码来源:main.go

示例7: GetKotobaFromRows

func GetKotobaFromRows(rows *sql.Rows) *[]Kotoba {
	// get data from rows
	var kotobaArray []Kotoba
	columns, err := rows.Columns()
	values := make([]sql.RawBytes, len(columns))
	scanArgs := make([]interface{}, len(values))
	for i := range values {
		scanArgs[i] = &values[i]
	}
	//var kotoba Kotoba
	for rows.Next() {
		err = rows.Scan(scanArgs...)
		if err != nil {
			panic(err.Error())
		}
		var kotoba Kotoba
		kotoba.Id = GetIntValue(values[0])
		kotoba.User_id = GetIntValue(values[1])
		kotoba.Goi = GetStringValue(values[2])
		kotoba.Hatsuon = GetStringValue(values[3])
		kotoba.Hatsuon_ = GetStringValue(values[4])
		kotoba.Imi = GetStringValue(values[5])
		kotoba.Imi_ = GetStringValue(values[6])
		kotoba.Level = GetIntValue(values[7])
		kotoba.Review = GetStringValue(values[8])
		kotoba.Unlocked = GetStringValue(values[9])
		//fmt.Println(kotoba)
		kotobaArray = append(kotobaArray, kotoba)
	}
	return &kotobaArray
}
开发者ID:serash,项目名称:YunYun,代码行数:31,代码来源:db_test.go

示例8: Show

// Show writes the sql.Rows given to the destination in tablewriter basic format.
func (prettyCsvOutput *PrettyCSVOutput) Show(rows *sql.Rows) {
	cols, colsErr := rows.Columns()

	if colsErr != nil {
		log.Fatalln(colsErr)
	}

	if prettyCsvOutput.options.WriteHeader {
		prettyCsvOutput.writer.SetHeader(cols)
		prettyCsvOutput.writer.SetHeaderLine(true)
		prettyCsvOutput.writer.SetAutoFormatHeaders(false)
	}

	rawResult := make([][]byte, len(cols))
	result := make([]string, len(cols))

	dest := make([]interface{}, len(cols))

	for i := range cols {
		dest[i] = &rawResult[i]
	}

	for rows.Next() {
		rows.Scan(dest...)

		for i, raw := range rawResult {
			result[i] = string(raw)
		}

		prettyCsvOutput.writer.Append(result)
	}

	prettyCsvOutput.writer.Render()
	rows.Close()
}
开发者ID:jmptrader,项目名称:textql,代码行数:36,代码来源:pretty_csv.go

示例9: DisplayResults

func DisplayResults(rows *sql.Rows) error {
	defer rows.Close()
	columns, err := rows.Columns()
	if err != nil {
		return err
	}

	var (
		w      = tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
		num    = len(columns)
		format = fmt.Sprintf("%s\n", getNumTabs(num))

		values = make([]interface{}, num)
		strs   = make([]string, num)
	)

	for i := 0; i < num; i++ {
		values[i] = &strs[i]
	}

	fmt.Fprintf(w, format, toInterface(columns)...)

	for rows.Next() {
		if err := rows.Scan(values...); err != nil {
			return err
		}
		fmt.Fprintf(w, format, toInterface(strs)...)
	}
	return w.Flush()
}
开发者ID:ikeee,项目名称:dockersql,代码行数:30,代码来源:display.go

示例10: ParseResults

func ParseResults(r *sql.Rows) ResultSet {
	cols, _ := r.Columns()

	var newMapSlice = ResultSet{}

	var counter int = 0
	for r.Next() {
		counter = counter + 1
		var newRow = make(Result)
		var scanargs = make([]interface{}, len(cols))
		var scanvals = make([]interface{}, len(cols))

		for i := range scanargs {
			scanargs[i] = &scanvals[i]
		}
		r.Scan(scanargs...)

		for i, columnname := range cols {
			if scanvals[i] != nil {
				if reflect.TypeOf(scanvals[i]).String() == "[]uint8" {
					newRow[columnname] = string(scanvals[i].([]byte))
				} else {
					newRow[columnname] = scanvals[i]
				}
			}
		}
		newMapSlice = append(newMapSlice, newRow)
	}
	return newMapSlice
}
开发者ID:jrbudnack,项目名称:cf-sql-broker,代码行数:30,代码来源:basedbconnection.go

示例11: GetResult

func GetResult(rows *sql.Rows) ([]interface{}, []string) {
	if rows == nil {
		panic("rows is null")
	}
	columns, err := rows.Columns()

	if err != nil {
		panic(err.Error()) // proper error handling instead of panic in your app
	}

	values := make([]interface{}, len(columns))
	scan_args := make([]interface{}, len(columns))
	for i := range values {
		scan_args[i] = &values[i]
	}

	var ret []interface{}
	for rows.Next() {
		err = rows.Scan(scan_args...)
		if err != nil {
			panic(err.Error()) // proper error handling instead of panic in your app
		}

		//TODO
		//row := makeRow(columns, scan_args...)
		//row := make(ResultRow, len(columns))
		//var row ResultRow
		row := ResultRow{}
		for i, col_name := range columns {
			row[col_name] = values[i]
		}
		ret = append(ret, row)
	}
	return ret, columns
}
开发者ID:JingxuanWang,项目名称:Playground,代码行数:35,代码来源:mysql_client.go

示例12: RowsToMap

func (m *Mysql) RowsToMap(rows *sql.Rows) (map[string]interface{}, error) {
	result := make(map[string]interface{})

	fields, err := rows.Columns()
	if err != nil {
		loger.Print(err.Error())
		return nil, err
	}

	var containers []interface{}
	for i := 0; i < len(fields); i++ {
		var container interface{}
		containers = append(containers, &container)
	}
	if err := rows.Scan(containers...); err != nil {
		return nil, err
	}

	for index, field := range fields {
		rawValue := reflect.Indirect(reflect.ValueOf(containers[index]))

		if rawValue.Interface() != nil {
			result[field] = rawValue.Interface()
		}
	}
	return result, nil
}
开发者ID:rainkid,项目名称:ago,代码行数:27,代码来源:mysql.go

示例13: CreateMapsFromRows

func CreateMapsFromRows(rows *sql.Rows, types ...interface{}) (RecordMapList, error) {
	var err error

	columnNames, err := rows.Columns()
	if err != nil {
		return nil, err
	}

	// create interface
	var values []interface{}
	var reflectValues []reflect.Value

	var results RecordMapList
	values, reflectValues = sqlutils.CreateReflectValuesFromTypes(types)

	for rows.Next() {
		var result = RecordMap{}
		if err = rows.Scan(values...); err != nil {
			return nil, err
		}
		for i, name := range columnNames {
			result[name] = reflectValues[i].Elem().Interface()
		}
		results = append(results, result)
	}
	return results, nil
}
开发者ID:NewsCatching,项目名称:gatsby,代码行数:27,代码来源:filler.go

示例14: getResultMap

func getResultMap(query *sql.Rows) (res map[int]map[string]string) {
	cols, err := query.Columns()
	if err != nil {
		log.Fatal(err)
	}
	scanArgs := make([]interface{}, len(cols))
	vals := make([][]byte, len(cols))
	for i, _ := range cols {
		scanArgs[i] = &vals[i]
	}
	//最后得到的map
	results := make(map[int]map[string]string)
	i := 0
	for query.Next() {
		err := query.Scan(scanArgs...)
		if err != nil {
			log.Fatal(err)
		}
		row := make(map[string]string) //每行数据

		for k, v := range vals { //每行数据是放在values里面,现在把它挪到row里
			key := cols[k]
			row[key] = string(v)
		}
		results[i] = row //装入结果集中
		i++
	}
	return results
}
开发者ID:qiangmzsx,项目名称:golang,代码行数:29,代码来源:mysqlDemo.go

示例15: QueryResult

func QueryResult(rows *sql.Rows) map[int]map[string]string {
	var result = make(map[int]map[string]string)
	columns, _ := rows.Columns()
	values := make([]sql.RawBytes, len(columns))
	scanargs := make([]interface{}, len(values))
	for i := range values {
		scanargs[i] = &values[i]
	}

	var n = 1
	for rows.Next() {
		result[n] = make(map[string]string)
		err := rows.Scan(scanargs...)

		if err != nil {
			fmt.Println(err)
		}

		for i, v := range values {
			result[n][columns[i]] = string(v)
		}
		n++
	}

	return result
}
开发者ID:cruboy,项目名称:gomysql,代码行数:26,代码来源:mysql.go


注:本文中的database/sql.Rows.Columns方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。