在Go中整合了一个MySQL到JSON的快速转储工具。但是,我发现我从数据库检索的所有内容都是一个[]byte数组。因此,我将所有内容编码为字符串,而不是本机JSON整数或布尔值。
[]byte
代码子集:
import ( "encoding/json" "database/sql" _ "github.com/go-sql-driver/mysql" ) func dumpTable(w io.Writer, table) { // ... rows, err := Query(db, fmt.Sprintf("SELECT * FROM %s", table)) checkError(err) columns, err := rows.Columns() checkError(err) scanArgs := make([]interface{}, len(columns)) values := make([]interface{}, len(columns)) for i := range values { scanArgs[i] = &values[i] } for rows.Next() { err = rows.Scan(scanArgs...) checkError(err) record := make(map[string]interface{}) for i, col := range values { if col != nil { fmt.Printf("\n%s: type= %s\n", columns[i], reflect.TypeOf(col)) switch t := col.(type) { default: fmt.Printf("Unexpected type %T\n", t) case bool: fmt.Printf("bool\n") record[columns[i]] = col.(bool) case int: fmt.Printf("int\n") record[columns[i]] = col.(int) case int64: fmt.Printf("int64\n") record[columns[i]] = col.(int64) case float64: fmt.Printf("float64\n") record[columns[i]] = col.(float64) case string: fmt.Printf("string\n") record[columns[i]] = col.(string) case []byte: // -- all cases go HERE! fmt.Printf("[]byte\n") record[columns[i]] = string(col.([]byte)) case time.Time: // record[columns[i]] = col.(string) } } } s, _ := json.Marshal(record) w.Write(s) io.WriteString(w, "\n") } }
我还需要将数据库表转储到json,这是我实现的方式:(与本主题中的另一个答案不同,所有内容都不是字符串,这要归功于这个答案:我可以正确获取整数字段)
func getJSON(sqlString string) (string, error) { rows, err := db.Query(sqlString) if err != nil { return "", err } defer rows.Close() columns, err := rows.Columns() if err != nil { return "", err } count := len(columns) tableData := make([]map[string]interface{}, 0) values := make([]interface{}, count) valuePtrs := make([]interface{}, count) for rows.Next() { for i := 0; i < count; i++ { valuePtrs[i] = &values[i] } rows.Scan(valuePtrs...) entry := make(map[string]interface{}) for i, col := range columns { var v interface{} val := values[i] b, ok := val.([]byte) if ok { v = string(b) } else { v = val } entry[col] = v } tableData = append(tableData, entry) } jsonData, err := json.Marshal(tableData) if err != nil { return "", err } fmt.Println(string(jsonData)) return string(jsonData), nil }
这是一个示例输出:
[{"ID":0,"Text":"Zero"},{"ID":1,"Text":"One"},{"ID":2,"Text":"Two"}]