OpenInformix / IfxPy

Informix native Python driver for scalable enterprise and IoT solutions.
Other
46 stars 22 forks source link

Python: [Informix][Informix ODBC Driver]Invalid string or buffer length. SQLCODE=-11071 when I fetch data #32

Closed Amrouane closed 4 years ago

Amrouane commented 4 years ago

When I try to retreive my table in informix with ifxpy package, I get this error:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-6-b0557e7f099b> in <module>
     16 while dictionary != False:
     17         tph.append(dictionary)
---> 18         dictionary = IfxPy.fetch_assoc(stmt)
     19 print(pd.DataFrame(tph))

Exception: [Informix][Informix ODBC Driver]Invalid string or buffer length. SQLCODE=-11071

This is my code:

import IfxPy
import pandas as pd
ConStr = "SERVER=informix1;DATABASE=ir_fmois;HOST=127.0.0.1;SERVICE=9092;UID=informix;PWD=1234;"
# netstat -a | findstr  9088
try:
    # netstat -a | findstr  9088
    conn = IfxPy.connect( ConStr, "", "")
except Exception as e:
    print ('ERROR: Connect failed')
    print ( e )
    quit()
sql = "SELECT * FROM oih"
stmt = IfxPy.exec_immediate(conn, sql)
dictionary = IfxPy.fetch_assoc(stmt)
tph=[]
while dictionary != False:
        tph.append(dictionary)
        dictionary = IfxPy.fetch_assoc(stmt)
print(pd.DataFrame(tph))

When I print the dataframe I see that I got only the first 4 rows.

I tried also this code and i doesn't trhow any exception but it returns also the first 4 rows of my table:

import IfxPyDbi as dbapi2
ConStr = "SERVER=informix1;DATABASE=ir_fmois;HOST=127.0.0.1;SERVICE=9092;UID=informix;PWD=1234;"
conn = dbapi2.connect( ConStr, "", "")
cur = conn.cursor()
sql = "SELECT * FROM oih"
rows = cur.fetchall()
len(rows)
>>>4

I tried importing columns one by one, and the same error has occured when I tried to select a byte (blob) column (with text data type). This column has emty values in the first 4 rows but the fifth wasn't empty, and I think this is why the error occured in line 5.

I would really appreciate if anyone has any idea on how to solve this.

jsagrera commented 4 years ago

Can we have your schema/data and CSDK version? This is what I tried (tblob(c1 int, c2 blob)

informix@irk:/data/informix/IBM/OpenInformix/Py$ python3 t3.py 
   c1                                                 c2
0   1                                               None
1   2                                               None
2   3                                               None
3   4                                               None
4   5  b'2020-01-03 15:26:49 17794 18897 Native Insta...
informix@irk:/data/informix/IBM/OpenInformix/Py$ 

> select * from tblob where c2 is not null;

c1  5
c2  <SBlob Data>

1 row(s) retrieved.

> 

And same with a byte column (tbyte(c1 int, c2 byte))

informix@irk:/data/informix/IBM/OpenInformix/Py$ python3 t3.py
   c1                                                 c2
0   1                                               None
1   2                                               None
2   3                                               None
3   4                                               None
4   5  b'2020-01-03 15:26:49 17794 18897 Native Insta...
informix@irk:/data/informix/IBM/OpenInformix/Py$ 

> select * from tbyte where c2 is not null;

c1  5
c2  <BYTE value>

1 row(s) retrieved.

> 

informix@irk:/data/informix/IBM/OpenInformix/Py$ python3 t3.py
   c1         c2
0   1       None
1   2       None
2   3       None
3   4       None
4   5  b'defghi'
informix@irk:/data/informix/IBM/OpenInformix/Py$ 

Testing the IfxPyDbi driver:

informix@irk:/data/informix/IBM/OpenInformix/Py$ cat t4.py 
import IfxPyDbi as dbapi2
ConStr = "SERVER=irk1210;DATABASE=stores7;HOST=irk;SERVICE=9088;UID=informix;PWD=ximrofni;"
conn = dbapi2.connect( ConStr, "", "")
cur = conn.cursor()
sql = "SELECT * FROM tblob"
cur.execute(sql)
rows = cur.fetchall()
len(rows)
print ("done ",rows)
informix@irk:/data/informix/IBM/OpenInformix/Py$ 

informix@irk:/data/informix/IBM/OpenInformix/Py$ python3 t4.py 
done  [(1, None), (2, None), (3, None), (4, None), (5, b'defghi')]
informix@irk:/data/informix/IBM/OpenInformix/Py$

informix@irk:/data/informix/IBM/OpenInformix/Py$ python3 t4.py 
done  [(1, None), (2, None), (3, None), (4, None), (5, b'2020-01-03 15:26:49 1.......')]
informix@irk:/data/informix/IBM/OpenInformix/Py$

Both appear to work fine for me.