本文整理汇总了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
}
示例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
}
示例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
}
示例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
}
示例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")
}
}
示例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,
}
}
示例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
}
示例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()
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}