By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Array Problem

P: 4
Hi everyone,
I am writing a python program using PyADO to add some data to an 'mdb'
database. For example,

filename = UserObjs.mdb
table I am working with = Lines
fields I am working with = ID and Metrics

The 'ID' field is just a number field. The 'Metrics' filed is an OLE object.
In this OLE object assigned in Microsoft Access, I need to include a series
of latitude/longitude numbers as in an array.

The problem I have is that, let's suppose I want to add this array:
[(23.00000,56.5897),(23.4569,58),(244569,60)]. I can add it with no problem to the OLE field, but...when I read the database (SELECT etc..), what I get is something like
this: [ *(*2*3*.*0*,...) ]

(Note: Here, I used the * to symbolize spaces.)

What I wanted was to store the array as a byte stream. How can I do that?

I'd appreciate any help I could get on this!

Thank you so much!!!

Regards,

Michel
PS: Below is the code I am using to insert / read the data:

****To insert:
Expand|Select|Wrap|Line Numbers
  1. import PyADO
  2. import win32com.client
  3. import sys
  4.  
  5. id_number = raw_input("ID? ")
  6.  
  7. id_metrics = [(23.00000,56.5897),(23.4569,58),(244569,60)]
  8.  
  9. conn = win32com.client.Dispatch(r'ADODB.Connection')
  10. DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
  11. SOURCE=C:\msm_test\OLE_project\UserObjs.mdb;'
  12. sql_statement = "INSERT INTO Lines (ID,Metrics) VALUES ('%s','%s');"
  13. %(id_number,id_metrics)
  14.  
  15. conn.Open(DSN)
  16. conn.Execute(sql_statement)
  17. conn.Close()
*** To read the data
Expand|Select|Wrap|Line Numbers
  1. import PyADO
  2.  
  3. conn =
  4. PyADO.connect(None,user='admin',password='',host=None,database='C:\\msm_test\OLE_project\UserObjs.mdb',provider='Microsoft.Jet.OLEDB.4.0')
  5.  
  6. curs = conn.cursor()
  7.  
  8. curs.execute("select * from Lines")
  9.  
  10. result = curs.fetchall()
  11. descr = curs.description
  12. for row in result:
  13.     for col in row:
  14.         print col
  15.  
  16. curs.close()
  17. conn.close()
Mar 24 '09 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Try storing the data as str objects.
Mar 24 '09 #2

P: 4
I have tried that and I get the same thing.

How do I convert my array to byte stream?
Mar 25 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I know next to nothing about DB/Python interfaces, so I am mostly guessing. Why not try parsing the returned string? If you want to store the data as a byte stream, you may need a different field type in the DB.

Example data parse using eval():
Expand|Select|Wrap|Line Numbers
  1. >>> returned_data = "[ (2 3 . 0 0 0 0 0 , 5 6 . 5 8 9 7 ) , ( 2 3 . 4 5 6 9 , 5 8 ) , (2 4 4 5 6 9 , 6 0 ) ]"
  2. >>> eval(returned_data.replace(" ", ""))
  3. [(23.0, 56.589700000000001), (23.456900000000001, 58), (244569, 60)]
Mar 25 '09 #4

P: 4
Even by including your suggestion, that is, using eval and replace, it still adds spaces between the data. And because of the spaces, I can't access the data correctly in another program.
The same thing happens if I Pickle the data before adding to the database.
Mar 25 '09 #5

P: 4
There's another test you can make:
test = (2,3)
import cPickle
testpickle = cPickle.dumps(test,2)
testpickle
print testpickle

The last two commands are very different. When you type just testpickle, you get something like:
'\x80\x02K\x02K\x03\x86q\x01.'
when you type 'print testpickle', you get:
€ K K † q .
There are spaces between the elements. These are the spaces I am mentioning here. This is how the dataset is being stored in the mdb database. How do I get rid of these 'null bytes'?
Thanks!
Mike
Mar 25 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.