Array Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikesm
    New Member
    • Mar 2009
    • 4

    Array Problem

    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.58 97),(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:
    Code:
    import PyADO
    import win32com.client
    import sys
    
    id_number = raw_input("ID? ")
    
    id_metrics = [(23.00000,56.5897),(23.4569,58),(244569,60)]
    
    conn = win32com.client.Dispatch(r'ADODB.Connection')
    DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
    SOURCE=C:\msm_test\OLE_project\UserObjs.mdb;'
    sql_statement = "INSERT INTO Lines (ID,Metrics) VALUES ('%s','%s');"
    %(id_number,id_metrics)
    
    conn.Open(DSN)
    conn.Execute(sql_statement)
    conn.Close()
    *** To read the data
    Code:
    import PyADO
    
    conn =
    PyADO.connect(None,user='admin',password='',host=None,database='C:\\msm_test\OLE_project\UserObjs.mdb',provider='Microsoft.Jet.OLEDB.4.0')
    
    curs = conn.cursor()
    
    curs.execute("select * from Lines")
    
    result = curs.fetchall()
    descr = curs.description
    for row in result:
        for col in row:
            print col
    
    curs.close()
    conn.close()
    Last edited by bvdet; Mar 24 '09, 10:44 PM. Reason: Add code tags and indentation
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Try storing the data as str objects.

    Comment

    • mikesm
      New Member
      • Mar 2009
      • 4

      #3
      I have tried that and I get the same thing.

      How do I convert my array to byte stream?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        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():
        Code:
        >>> 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 ) ]"
        >>> eval(returned_data.replace(" ", ""))
        [(23.0, 56.589700000000001), (23.456900000000001, 58), (244569, 60)]

        Comment

        • mikesm
          New Member
          • Mar 2009
          • 4

          #5
          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.

          Comment

          • mikesm
            New Member
            • Mar 2009
            • 4

            #6
            There's another test you can make:
            test = (2,3)
            import cPickle
            testpickle = cPickle.dumps(t est,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

            Comment

            Working...