greenew / pypyodbc

Automatically exported from code.google.com/p/pypyodbc
0 stars 0 forks source link

Using pypyodbc on Mac #31

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Just open a simple python file and paste this 

#!/usr/bin/env python

import sys
import httplib
import os.path

import pypyodbc
cnxn = pypyodbc.connect('DSN=servername;UID=username;PWD=password')

(use a valid servername/uid/password above ..I just put in placeholders)
This should be done on the latest Mac OSX .

What is the expected output? What do you see instead?
The expected out is that the connection should be established.Instead I see 
something like this 

[iODBC][Driver Manager]Data source name not found and no default driver 
specified. Driver could not
Traceback (most recent call last):
  File "./myfile.py", line 8, in <module>
    cnxn = pypyodbc.connect('DSN=servername;UID=username;PWD=password' )
  File "/../../pypyodbc.py", line 2329, in __init__
    self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)
  File "/../../pypyodbc.py", line 2394, in connect
    check_success(self, ret)
  File "/../../pypyodbc.py", line 995, in check_success
    ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
  File "/../../pypyodbc.py", line 982, in ctrl_err
    err_list.append((from_buffer_u(state), from_buffer_u(Message), NativeError.value))
  File "/../../pypyodbc.py", line 486, in UCS_dec
    uchar = buffer.raw[i:i + ucs_length].decode(odbc_decoding)
  File "/../../Python.framework/Versions/2.7/lib/python2.7/encodings/utf_32.py", line 11, in decode
    return codecs.utf_32_decode(input, errors, True)
UnicodeDecodeError: 'utf32' codec can't decode bytes in position 0-1: truncated 
data

What version of the product are you using? On what operating system?

Using the latest pypyodbc.This is MacOSX.

Please provide any additional information below.

I installed freeTDS and unixODBC using macports.The tsql and isql show the 
connection properly (and are under /opt/local/) .I am not sure if pypyodbc is 
able to detect them?

I did look into this issue that was already reported 

http://code.google.com/p/pypyodbc/issues/detail?id=20

and my pypyodbc.py has 

 # only iODBC uses utf-32 / UCS4 encoding data, others normally use utf-16 / UCS2
        # So we set those for handling.
        if 'libiodbc.dylib' in library:
            odbc_decoding = 'utf_32'
            odbc_encoding = 'utf_32_le'
            ucs_length = 4

so that should be taken care of .

The ODBC api name/usr/lib/libiodbc.dylib.

Original issue reported on code.google.com by rajesh...@gmail.com on 18 Dec 2013 at 2:45

GoogleCodeExporter commented 8 years ago
I was able to figure out the reason for the

"[iODBC][Driver Manager]Data source name not found and no default driver 
specified. Driver could not"

error.I added the proper odbc.ini and odbcinst.ini but the original decode 
error still persists.

This call from pypyodbc.py is failing 

try:
                lock.acquire()
                ret = odbc_func(self.dbc_h, 0, c_connectString,len(self.connectString), None, 0, None, SQL_DRIVER_NOPROMPT)

            finally:
                lock.release()

The ret value is -1 which corresponds to SQL_ERROR. And the odbc_func is 
"ODBC_API.SQLDriverConnectW"

Original comment by rajesh...@gmail.com on 19 Dec 2013 at 12:56

GoogleCodeExporter commented 8 years ago
Also I further added debugging to identify the type of specific sql state we 
were seeing.
Since we seem to be going through the non ansi part right before calling the 
above odbc_connect function looking at the rest of your file I added this

try:
                lock.acquire()
                ret = odbc_func(self.dbc_h, 0, c_connectString, len(self.connectString), None, 0, None, SQL_DRIVER_NOPROMPT)
                if ret == SQL_ERROR:
                        stateE = create_buffer_u(22)
                        MessageE = create_buffer_u(1024*4)
                        ODBC_funcE = ODBC_API.SQLGetDiagRecW
                        raw_sE = unicode
                        NativeErrorE = ctypes.c_int()
                        Buffer_lenE = c_short()
                        val = ODBC_funcE(SQL_HANDLE_DBC, self.dbc_h,1, stateE,ADDR(NativeErrorE), MessageE, 1024, ADDR(Buffer_lenE))
                        print val
                        print MessageE.value
            finally:
                lock.release()

The value of 'val' printed is 0.And MessageE.value is '['.StateE.value is 0. 
NativeErrorE.value is 20013(but I dont find any interpretation for that here?)

Does the implementation of ODBC_API.SQLGetDiagRecW here look correct?

Original comment by rajesh...@gmail.com on 19 Dec 2013 at 1:58

GoogleCodeExporter commented 8 years ago
I get the exact same issue as well.

See: https://gist.github.com/therealprologic/5ce32e33c8f51d8bf01b

Original comment by j.mi...@griffith.edu.au on 2 Jul 2014 at 1:06

GoogleCodeExporter commented 8 years ago
Although this specific error above goes away when I:

brew install unixodb

But then I get a different error:

https://gist.github.com/therealprologic/5ce32e33c8f51d8bf01b#comment-1256068

Original comment by j.mi...@griffith.edu.au on 2 Jul 2014 at 1:10