-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathListArray.go
42 lines (40 loc) · 958 Bytes
/
ListArray.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package gpa
import (
"database/sql"
"github.com/cihub/seelog"
)
func (dao *Gpa) ListArrayString(sqlString string, param ...interface{}) ([][]string, error) {
defer func() {
if err := recover(); err != nil {
seelog.Error("Query fail.\n\t", sqlString , param, "\n", err)
}
}()
rows, err := dao.Conn.Query(sqlString, param...)
defer rows.Close()
if err == nil {
if cols, ec := rows.Columns(); ec == nil {
var result [][]string
for rows.Next() {
result = append(result, rowToStringArray(rows, cols))
}
return result, nil
}
} else {
seelog.Error("数据库查询出错:", dao.dsn, err)
return nil, err
}
return nil, nil
}
func rowToStringArray(rows *sql.Rows, cols []string) []string {
arr, _ := scan(rows, cols)
res := make([]string, len(cols))
for i := 0; i < len(cols); i++ {
v := arr[i].(*sql.NullString)
if v.Valid {
res[i] = arr[i].(*sql.NullString).String
} else {
res[i] = ""
}
}
return res
}