Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i query with bit field in where clause #1608

Open
superceix opened this issue Jul 18, 2024 · 1 comment
Open

how can i query with bit field in where clause #1608

superceix opened this issue Jul 18, 2024 · 1 comment

Comments

@superceix
Copy link

table info

-- table define
CREATE TABLE `t0` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` int(11) NOT NULL,
  `c2` varchar(64) COLLATE utf8mb4_bin NOT NULL,
  `c3` bit(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

-- row insert
insert into t0 values (1,1,'aaa',b'1100');

Example code

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	connStr := "xxx:xxx@tcp(xxxxx:xxxx)/?timeout=3600s"
	if conn, err := sql.Open("mysql", connStr); err != nil {
		fmt.Print("ReCreate conn failure!", err.Error())
	} else {
		sql1 := "SELECT c3 FROM `test1`.`t0` WHERE `c2`=?"
		rows1 := conn.QueryRow(sql1, "aaa")
		c3 := []byte{}
		rows1.Scan(&c3)  // row scan success, and get c3 is '[0] = 12 = 0xc' in debug

		sql := "SELECT c2 FROM `test1`.`t0` WHERE `c3`=?"
		rows, err := conn.Query(sql, c3)
		if err != nil {
			fmt.Printf("quert errpor")
		}
		for rows.Next() {
			var c2 string
			if err := rows.Scan(&c2); err != nil {
				fmt.Print("Row.Scan rawRecord Error", err)
			}
			fmt.Printf("c2:%s", c2)
		}
	}
}

no rows result after conn.Query(sql, c3)

@methane
Copy link
Member

methane commented Jul 19, 2024

Try query with mysql cli first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants