Python Forum
SQL varbinary data type retrieval in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL varbinary data type retrieval in python
#1
I'm working on python scripts to store and retrieve some videos and images from an SQL database, I stored a jpg file on the server as a varbinary type. However when I SELECT the record it returns an address b'\xff\xd8\xff\xe1o\x9eExif\x00\x00II*\x00\x08\x00\x00\x00\x0c\x00\x0f\x01\x02\x00\n\x00\x00\x00\x9.... (as expected) I'm wondering how exactly can I go about converting this back to an image. I would like to to test both streaming the image and writing it to disk and storing it locally.

here is my insertion code that successfully stores the image, I'm using pyodbc

#Insertion query 
insertion = "INSERT INTO " + '"Shubert Robots & Electrical Panels"' + " (Path, Filename, ID, Image)" + '\n'
insertion += "SELECT " + Path + ", " + Name + ", " + str(ID) + ", BulkColumn" +'\n'
insertion += "FROM Openrowset( Bulk " +  Image + ", " + "Single_Blob) as Image;" 
print(insertion + '\n')
cur.execute(insertion)


Here is my select code which returns the address
#select query
select = 'SELECT * FROM "Shubert Robots & Electrical Panels" WHERE ID = 0;'
print(select + '\n')
cur.execute(select)
values = cur.fetchall()
Any help is appreciated thank you.
Reply
#2
Are you SURE your table name is "Shubert Robots & Electrical Panels"? If you're still early in development, you should fix that as soon as possible lol.

Anyway, can you just open a file in binary mode, and write the image out? Something like:
value = b'' # however you get it from the db
with open("the_img.jpg", "wb") as img:
  img.write(value)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,956 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,389 Jan-30-2021, 07:11 AM
Last Post: alloydog
  How to iterate dict_values data type nisusavi 2 7,517 Apr-17-2020, 08:06 PM
Last Post: nisusavi
  What is the meaning of mutable data type? qliu 3 2,953 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  Data Type conversion error rajeevjagatap 2 4,343 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Type hinting - return type based on parameter micseydel 2 2,490 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Reading in MYSQL data as integer type Daveinthebigcity 2 3,583 May-13-2019, 11:30 AM
Last Post: Daveinthebigcity
  Type error when reading in different data types on an __init__ method Dylanmull 3 2,806 May-09-2019, 02:05 PM
Last Post: buran
  scatter plot error - possibly data type - TypeError: nan is not a string chudson 1 6,528 Mar-24-2019, 11:48 AM
Last Post: chudson
  Storing MySQL BIT Data Type data in python variable krushna 2 3,500 Dec-31-2018, 01:28 AM
Last Post: krushna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020