Created
December 6, 2022 04:19
-
-
Save kkdai/05a73a1a97a63ad20a645848f2730859 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 建立与数据库的连接 | |
| db, err := sql.Open("mysql", "user:password@tcp(host:port)/dbname") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer db.Close() | |
| // 查询数据库 | |
| rows, err := db.Query("SELECT * FROM users") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer rows.Close() | |
| // 获取列元数据 | |
| columnTypes, err := rows.ColumnTypes() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // 打印每一列的元数据信息 | |
| for _, columnType := range columnTypes { | |
| fmt.Println("Name:", columnType.Name()) | |
| fmt.Println("DatabaseTypeName:", columnType.DatabaseTypeName()) | |
| fmt.Println("Length:", columnType.Length()) | |
| fmt.Println("Nullable:", columnType.Nullable()) | |
| fmt.Println("Precision:", columnType.Precision()) | |
| fmt.Println("ScanType:", columnType.ScanType()) | |
| fmt.Println("------------------------------") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment