Scan []byte from table

My table structure has blob column that means []byte. when i was trying to scan it it displays ----- unsupported column type -98 ----- what is it can you explain me.

Thanks in advance.

We need you to provide more information to be able to help you:

What RDBM are you using? Which driver are you using? Did you check its docs (it may very well not support blobs)? Etc.

@iegomez
Yes! I am using Db2(IBM database) and i have created a table with column type blob. which can store byte array. Now i have use QueryRow to Scan the value and returns “unsupported type -98”.

_,err=db.Query(“create table byt1(name blob(5))”)
Insertion Commands…then

var a [5]byte
err = db.QueryRow(“select name from byt1”).Scan(&a)

Now it returns “unsupported column type -98”

From database/sql package:

The sql package must be used in conjunction with a database driver. See https://golang.org/s/sqldrivers for a list of drivers.

So, which is it? Or are you using something else?

I am using https://bitbucket.org/phiggins/db2cli package.

Checking the code at column.go, the NewColumn function doesn’t seem to support blobs, though binary, varbinary and longvarbinary should be supported:

    switch sqltype {
	case api.SQL_BIT:
		return NewBindableColumn(b, api.SQL_C_BIT, 1), nil
	case api.SQL_TINYINT, api.SQL_SMALLINT, api.SQL_INTEGER:
		return NewBindableColumn(b, api.SQL_C_LONG, 4), nil
	case api.SQL_BIGINT:
		return NewBindableColumn(b, api.SQL_C_SBIGINT, 8), nil
	case api.SQL_NUMERIC, api.SQL_DECIMAL, api.SQL_FLOAT, api.SQL_REAL, api.SQL_DOUBLE:
		return NewBindableColumn(b, api.SQL_C_DOUBLE, 8), nil
	case api.SQL_TYPE_TIMESTAMP:
		var v api.SQL_TIMESTAMP_STRUCT
		return NewBindableColumn(b, api.SQL_C_TYPE_TIMESTAMP, int(unsafe.Sizeof(v))), nil
	case api.SQL_TYPE_DATE:
		var v api.SQL_DATE_STRUCT
		return NewBindableColumn(b, api.SQL_C_DATE, int(unsafe.Sizeof(v))), nil
	case api.SQL_CHAR, api.SQL_VARCHAR:
		return NewVariableWidthColumn(b, api.SQL_C_CHAR, size), nil
	case api.SQL_WCHAR, api.SQL_WVARCHAR:
		return NewVariableWidthColumn(b, api.SQL_C_WCHAR, size), nil
	case api.SQL_BINARY, api.SQL_VARBINARY:
		return NewVariableWidthColumn(b, api.SQL_C_BINARY, size), nil
	case api.SQL_LONGVARCHAR:
		return NewVariableWidthColumn(b, api.SQL_C_CHAR, 0), nil
	case api.SQL_WLONGVARCHAR, api.SQL_SS_XML:
		return NewVariableWidthColumn(b, api.SQL_C_WCHAR, 0), nil
	case api.SQL_LONGVARBINARY:
		return NewVariableWidthColumn(b, api.SQL_C_BINARY, 0), nil
	default:
		return nil, fmt.Errorf("unsupported column type %d", sqltype)
	}

That said, the driver you’re using hasn’t been updated in over 3 years, so I’d look for a new and hopefully maintained one (I don’t use DB2, so no idea which one could help).

@iegomez

Thankyou very much.

Do you know how go get works and can we control the process

If you are referring to this topic, or this one, keep those discussions there. Please don’t mix discussions in unrelated topics.

ok…thanks.

SQL_BLOB is SQL_LONGVARBINARY which is in the switch case but why it is not able to found.

That depends on the C API that’s being called. You may see these include and constants at db2cli/api/api_unix.go (notice how there’s no BLOB):

// #include <sqlcli1.h>

...
const(
...
	SQL_BINARY          = C.SQL_BINARY
	SQL_VARBINARY       = C.SQL_VARBINARY
	SQL_LONGVARBINARY   = C.SQL_LONGVARBINARY
...
)

If you google that header, the first result shows that BLOB is a valid data type, but doesn’t mention the BINARY ones:

/* SQL extended data types */
#define  SQL_GRAPHIC            -95
#define  SQL_VARGRAPHIC         -96
#define  SQL_LONGVARGRAPHIC     -97
#define  SQL_BLOB               -98
#define  SQL_CLOB               -99

Notice also how -98 matches your error’s column type.

So it seems that long ago the sqlcli1.h header declared binary types and now declares a blob one, and the values between them don’t match. So the outdated driver correctly reports that the column type is unknown as it doesn’t implement the new API. In other words, no, BLOB doesn’t translate to LONGVARBINARY in this case.

You could patch this yourself if you wanted to (though you’ll probably find a lot of these errors trying to make the driver work), but I recommend you find a newer and maintained driver.

Thank You.

@iegomez
I have just added that SQL_BLOB in column.go and api_windows.go file then it is producing "sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *[5]uint8 " yhis type error do you know what to do with this error.

The type required is a slice but you give it an array. Try defining a slice with a capacity of 5 and give it as an argument

v := make([]uint8, 0, 5)

Thanks

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.