Skip to content

Empty byte array is not NULL #424

Closed
Closed
@xxr3376

Description

@xxr3376

[]byte{} should be converted to empty BLOB instead of NULL, []byte{} != nil in go.

I use following table to reproduce my issue.

CREATE TABLE test (
    field BLOB NOT NULL
);

Following code will report NOT NULL constraint failed error, but it's ok to execute INSERT INTO test (field) VALUES ('') in command line tool.

_, err := db.Exec(`INSERT INTO test (field) VALUES (?)`, []byte{})
// err = "NOT NULL constraint failed"

https://github.com/mattn/go-sqlite3/blob/master/sqlite3.go#L776 calls sqlite3_bind_blob with buffer=nil when len(buffer) = 0, but according to Sqlite C Interface, it shouldn't.

If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16() or sqlite3_bind_blob() is a NULL pointer then the fourth parameter is ignored and the end result is the same as sqlite3_bind_null().

Maybe we should make([]byte, 1) then pass its address and length=0 to sqlite3_bind_blob.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions