jangbagu / pypyodbc

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

Values of bit type are read incorrectly #16

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. In SQL Server have a table with the column of bit type, and some rows where 
the value of that column is 1.
2. Write a simple code retrieving the data.
3. Run the script.

What is the expected output? What do you see instead?
Expected output is value "True" for that column: it is converted automatically 
to the Python "bool" type, so "1" should become "True".
Instead, I see "False".

What version of the product are you using? On what operating system?
Windows7, Python 3.3.2, pypyodbc-1.1.1-py3.3, SQL Server 10.0.4000, 
driver "SQL Server Native Client 10.0"

Please provide any additional information below.

Original issue reported on code.google.com by lilyev...@gmail.com on 5 Jul 2013 at 3:03

GoogleCodeExporter commented 8 years ago
Just re-tested it on Linux, with both native microsoft driver and FreeTDS. The 
same thing happens, so the problem is certainly in pypyodbc code.

Original comment by lilyev...@gmail.com on 5 Jul 2013 at 4:46

GoogleCodeExporter commented 8 years ago
I think I found the bug. This will be one character fix.

In pypyodbc.py in line 578, instead of

  lambda x:x=='1'

should be:

  lambda x:x==b'1'

Can somebody do an official code fix?

Original comment by lilyev...@gmail.com on 5 Jul 2013 at 5:43

GoogleCodeExporter commented 8 years ago
Hi, Thanks for reporting this and the fix.
I have updated the source code in GitHub:

https://github.com/jiangwen365/pypyodbc/blob/master/pypyodbc.py

Original comment by jiangwen...@gmail.com on 6 Jul 2013 at 2:37

GoogleCodeExporter commented 8 years ago
Sorry, your fix does not work in Python3. You effectively call:
   bytes('1')
which gives an error:
   TypeError: string argument without an encoding
I guess, you did not try the fix in Python3.

If you use bytes() function, it should be bytes('1', 'ascii')
I understand it is a little tricky to support both Python2 and Python3.
For me, the "b'1'" worked fine. Can you try it in Python2? If it works, then 
you can fix it the way I suggested. 

Original comment by lilyev...@gmail.com on 8 Jul 2013 at 4:29

GoogleCodeExporter commented 8 years ago
Or you can simply fix the line 42 like this:

       str_8b = lambda s: bytes(s, 'ascii')

Original comment by lilyev...@gmail.com on 8 Jul 2013 at 4:44

GoogleCodeExporter commented 8 years ago
Hi, yes, that does work, I have updated the source code in GitHub:

https://github.com/jiangwen365/pypyodbc/blob/master/pypyodbc.py

Can you try again and see if it works?

Original comment by jiangwen...@gmail.com on 9 Jul 2013 at 1:29

GoogleCodeExporter commented 8 years ago
I already tried this by fixing the code in my own copy of the module.

Original comment by lilyev...@gmail.com on 9 Jul 2013 at 12:16

GoogleCodeExporter commented 8 years ago
fixed in version 1.1.5

Original comment by jiangwen...@gmail.com on 11 Jul 2013 at 12:04