- 
  Notifications
 You must be signed in to change notification settings 
- Fork 2.3k
-
Setup:
CREATE TABLE bit_columns ( bit1_col BIT(1), bit8_col BIT(8), bit16_col BIT(16), bit64_col BIT(64) );
Example:
package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/dbname") if err != nil { log.Fatal(err) } defer db.Close() rows, err := db.Query("SELECT * FROM bit_columns LIMIT 1") if err != nil { log.Fatal(err) } defer rows.Close() colTypes, err := rows.ColumnTypes() if err != nil { log.Fatal(err) } for _, colType := range colTypes { fmt.Println(colType.Name()) fmt.Println(colType.DecimalSize()) fmt.Println(colType.DatabaseTypeName()) fmt.Println(colType.Length()) } }
Output:
bit1_col
0 0 false
BIT
0 false // <- why 0?
bit8_col
0 0 false
BIT
0 false // <- why 0?
bit16_col
0 0 false
BIT
0 false // <- why 0?
bit64_col
0 0 false
BIT
0 false // <- why 0?
I would expect that the Length() method would return the mysql specified bit size, or at least have some way to get that piece of information from *sql.ColumnType.
I'm using github.com/go-sql-driver/mysql v1.8.1.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
Note that it returns false too. It means we can not get the value.
MySQL returns length in their resultset header. But its meaning is differ from database/sql's.
So we do not support ColumnType.Length(). It returns false always.
Beta Was this translation helpful? Give feedback.
All reactions
-
Oh, what a shame, thanks for the quick answer anyways.
So the only way to get that N would be to obtain it via a separate query then?
Beta Was this translation helpful? Give feedback.