Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BIT(N) columns don't include the N size in their types. #1674

Unanswered
alarbada asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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.

You must be logged in to vote
1 reply
Comment options

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1672 on January 30, 2025 02:44.

AltStyle によって変換されたページ (->オリジナル) /