jiangwen365 / pypyodbc

A pure Python Cross Platform ODBC interface module
MIT License
179 stars 75 forks source link

MSSQL: None instead of empty string returned #70

Open aatwork opened 6 years ago

aatwork commented 6 years ago

Hello,

pypyodbc_issue_rename_to_py.txt

I have a problem with a MSSQL varchar column value that should be an empty string but it is actually returned as None..

I have stripped the problem down to the following code: `import pypyodbc as pyodbc def get_db_cursor(db_host="server.domain.invalid,3180", db_name="db_name", db_user="user", db_password="pass" ): connection_string = "Driver={SQL Server};Server=" + db_host + ";Database=" + db_name + ";UID=" + db_user + ";PWD=" + db_password + ";" db = pyodbc.connect(connection_string) cursor = db.cursor() return db, cursor def main(): db, cursor = get_db_cursor() query = """SELECT NULL as nul_col, '' as empty_col""" for row in cursor.execute(query): print row

if name == "main": main()`

I fail to understand why this prints (None, None) instead of (None,'')

Is there a way to make pypyodbc it return (None, '') in this case?

Thank you for your help!

braian87b commented 6 years ago

We should see and debug the def fetchone(self): maybe is

elif alloc_buffer.value == '':
    value_list.append(None)
braian87b commented 6 years ago

yes, if you edit it to:

elif alloc_buffer.value == '':
    value_list.append('')

will output: Row(nul_col=None, empty_col='') (I had NamedTupleRow for sqlalchemy pyodbc compatibility so it is:) (None, '') in your case.

Good finding!!! Thanks!, I don't know if this break something else or not, but feel free to check by yourself and keep us posted, if It works well for you please post a PullRequest so @jiangwen365 will be able to merge this fix in the master branch.

braian87b commented 6 years ago

@aatwork have you tried it ?

ehassler commented 6 years ago

When I print the repr(cur.fetchall()) of the following

cur.execute('SELECT NULL AS "a", \'\' AS "b"') using pyodbc I get [(None, u'')] using pypyodbc 1.3.4 I get [(None, None)] using Python 2.7 on Mac or Linux, ODBC Driver 13 or 17 for SQL Server.

I cloned 1.3.6 and tried it and reproduced the problem there too. @braian87b 's suggestion of changing line 1909 of pypyodbc.py to value_list.append('') causes the result to match pyodbc.

I submitted a PR with @braian87b 's solution.

braian87b commented 6 years ago

Thanks!