问题
I'm trying to create an mbtiles server in Go. The file follows the mbtiles spec.
The tile_data field is a BLOB and i'm trying to query the database and get the corresponding images (stored as blob types).
So far the queries are OK but i'm getting corrupted tile_data results.
I'm not sure how to map a SQLite BLOB to a proper Go data structure.
So far i tried using var tileData []byte without success following this github gombtiles sample. The return value gets me an array of only 4 bytes which i suspect is kinda true since when i see the BLOB as Text type in a SQLite GUI it gets me 4 characters too (something like ÿØÿà)
To summarize:
The tile_data is a BLOB (storing an image) and i can't get a proper conversion to a Go var.
How can i convert a BLOB to Image in Go ?
UPDATE:
The ÿØÿà characters are 0xFF 0xD8 0xFF 0xE0 which are indeed the start of the JPEG file. I'm using the same MBTile file that i use in an iOS app through the Mapbox iOS SDK, so the file isn't corrupted and i can actually see the BLOB images using a SQLite GUI. The MBTiles spec says that the field is a BLOB type, and indeed, that's the field type that i'm using.
Again, the database is fine, is the same one used by an external iOS app. I can even query other data with success.
For context this is my isolated code.
func TilesHandler(w http.ResponseWriter, r *http.Request) {
db, _ := sql.Open("sqlite3", "./mapSource.mbtiles")
defer db.Close()
rows, _ := db.Query("SELECT tile_data FROM tiles WHERE zoom_level = 10 AND tile_column = 309 AND tile_row = 569")
defer rows.Close()
var tileData []byte
for rows.Next() {
rows.Scan(&tileData)
}
w.Write(tileData)
}
The zoom and coordinates are hardcoded for simplicity. tiledata is returning the mentioned 4 bytes.
UPDATE 2:
Here is the output of one specific record of the tiles table. Notice that the tile_data field is a BLOB and at the bottom the GUI shows that 4 characters. That are the exact one that i'm receiving in the []byte array at my Go code.
So my question is: I have this BLOB image file in my database which is a jpeg image. How i read it and show it as an image in my web page ? I want to read that BLOB and return an image in my http request.
Tiles row sample:
Tiles fields types
回答1:
This was a bug in the sqlite3 driver. It wasn't handling strings that contain NUL characters properly. It is now fixed.
Also the data have not been inserted correctly into the database. The datatype for them is TEXT and not BLOB as required by the schema. You can see this in the sqlite shell with: SELECT typeof(tile_data) FROM tiles;. 1
来源:https://stackoverflow.com/questions/29452538/convert-blob-to-image-in-go