Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:24:17+00:00 2026-06-01T07:24:17+00:00

FULL EDIT: I urgently need to access a Microsoft SQL Server and read compressed

  • 0

FULL EDIT:

I urgently need to access a Microsoft SQL Server and read compressed data from it, doing it with Python. After having had a lot of trouble, I finally found a working Matlab implementation, which does the job. I need it in Python however.

The data is stored in the image data type and has to be decompressed after downloading.
This is working Matlab code (using java routines) to do the job:

connection = actxserver('ADODB.Connection');
conString = 'Provider=SQLOLEDB; Data Source=adress.to.server; Integrated Security=SSPI; Initial Catalog=';
connection.ConnectionString = conString;
connection.CursorLocation = 'adUseClient';
connection.Open();
query_string = 'select Zip from Database where DatabaseName=''foo'' and Item=''bar'' ';
return = connection.Execute(query_string);
row = return.GetRows();
data = row{1};
class(data) % returns uint8
a = java.io.ByteArrayInputStream(data);
b = java.util.zip.InflaterInputStream(a);
isc = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier();
c = java.io.ByteArrayOutputStream;
isc.copyStream(b,c);
result = typecast(c.toByteArray,'uint16');

result now contains an array of integers that are the uncompressed desired data. I would like to achieve the same thing using Python. Here is the code that I am using to retrieve the data:

import pyodbc
connect = pyodbc.connect(Driver="SQL Server", Server="address.to.server")
cursor = connect.cursor()
cursor.execute("select Zip from database where DatabaseName='foo' and Item='bar'")
row = cursor.fetchone()
data = row[0] 
type data # returns <type 'bytearray'>

Now how do I inflate this bytearray? I tried

zlib.decompress(io.BytesIO(data).read())

which returns a bytearray of the correct length, but I need the array of integers that are returned by the above Matlab code. I tried decoding both the compressed and the inflated bytearray, but did not succeed.

Is there a difference on how Matlab and pyodbc handle the image data type in SQL? How can I retrieve the array of integers in Python?

I am using Python 2.7.2, pyodbc 3.0.5 and Matlab R2011b on Windows XP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:24:18+00:00Added an answer on June 1, 2026 at 7:24 am

    It sounds like you want to unpack things into unsigned 16-bit integers?

    So, you have something like:

    bytearray(b'\x01\x00\x02\x00\x03\x00\x04\x00')
    

    And you want:

    [1, 2, 3, 4]
    

    If so, you have several options.

    If you’re going to be using numpy anyway, consider using numpy for this.

    import numpy as np
    dat = bytearray(b'\x01\x00\x02\x00\x03\x00\x04\x00')
    data = np.frombuffer(buffer(dat), dtype=np.uint16)
    

    Alternately, you could do something like this using python’s builtin array:

    import array
    dat = bytearray(b'\x01\x00\x02\x00\x03\x00\x04\x00')
    data = array.array('H')
    data.fromstring(buffer(dat))
    

    You could also use the struct module, but it’s less than ideal for repetitive data like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Full edit: The scenario is that after uploading the file to the server via
EDIT: Removed the files from the server http://www.jdxsolutions.com/newsite/index.html http://www.jdxsolutions.com/newsite/default.css This is a layout I've
Edit: You can get the full source here: http://pastebin.com/m26693 Edit again: I added some
The full message that I got on the console is: Invalid memory access of
vector<Flight> flights; while (!myReadFile.eof()) { flights.push_back(*(new Flight())); // read some info... } after the
I have the following SQL code: select val.PersonNo, val.event_time, clg.number_dialed from vicidial_agent_log val join
Edit : See my full answer at the bottom of this question. tl;dr answer
EDIT: Posting full code (except XML as it a bunch of ridiculous table formatting!)
Full disclaimer: I'm a CS student, and this question is related to a recently
Full disclosure : This is for a homework assignment. This is driving me nuts.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.