diff --git a/sqlite3.go b/sqlite3.go index 088b110..d8f7756 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -169,7 +169,7 @@ func (s *SQLiteStmt) bind(args []interface{}) error { rv = C.sqlite3_bind_int(s.s, n, C.int(v)) case bool: if bool(v) { - rv = C.sqlite3_bind_int(s.s, n, -1) + rv = C.sqlite3_bind_int(s.s, n, 1) } else { rv = C.sqlite3_bind_int(s.s, n, 0) } diff --git a/sqlite3_test.go b/sqlite3_test.go index 035ffa8..a76d86f 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -209,3 +209,54 @@ func TestDelete(t *testing.T) { t.Errorf("Fetched row but expected not rows") } } + +func TestBooleanRoundtrip(t *testing.T) { + db, err := sql.Open("sqlite3", "./foo.db") + if err != nil { + t.Errorf("Tailed to open database:", err) + return + } + defer os.Remove("./foo.db") + + _, err = db.Exec("DROP TABLE foo") + _, err = db.Exec("CREATE TABLE foo(id INTEGER, value BOOL)") + if err != nil { + t.Errorf("Failed to create table:", err) + return + } + + _, err = db.Exec("INSERT INTO foo(id, value) VALUES(1, ?)", true) + if err != nil { + t.Errorf("Failed to insert true value:", err) + return + } + + _, err = db.Exec("INSERT INTO foo(id, value) VALUES(2, ?)", false) + if err != nil { + t.Errorf("Failed to insert false value:", err) + return + } + + rows, err := db.Query("SELECT id, value FROM foo") + if err != nil { + t.Errorf("Unable to query foo table:", err) + return + } + + for rows.Next() { + var id int + var value bool + + if err := rows.Scan(&id, &value) ; err != nil { + t.Errorf("Unable to scan results:", err) + continue + } + + if id == 1 && !value { + t.Errorf("Value for id 1 should be true, not false") + + } else if id == 2 && value { + t.Errorf("Value for id 2 should be false, not true") + } + } +}