jiangwen365 / pypyodbc

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

Decimal('0.015380') causes ValueError: byte string too long #72

Open dwaynePankey opened 6 years ago

dwaynePankey commented 6 years ago

Trying to set the parameter for and insert statement: Decimal('0.015380') Causes: pypyodbc.py, line 1614, in execute param_buffer.value = c_char_buf ValueError: byte string too long

dwaynePankey commented 6 years ago

The issue appears to be in the function: def execute(self, query_string, params=None, many_mode=False, call_mode=False):

These lines don't account for a decimal having leading zeros: if dec_num > 0:

has decimal

                    # 1.12 digit_num = 3 dec_num = 2
                    # 0.11 digit_num = 2 dec_num = 2 
                    # 0.01 digit_num = 1 dec_num = 2
                    left_part = digit_string[:digit_num - dec_num]
                    right_part = digit_string[0-dec_num:].zfill(dec_num)
                    v = ''.join((sign, left_part,'.', right_part))
dwaynePankey commented 6 years ago

This should resolve the issue:

                if dec_num > 0:
                    # has decimal
                    if digit_num >= dec_num:
                        left_part = digit_string[:digit_num - dec_num]
                        right_part = digit_string[0-dec_num:]
                    else:
                        #pad the right with zeros
                        left_part = ''
                        right_part = '0'*(dec_num - digit_num) + digit_string
                    v = ''.join((sign, left_part,'.', right_part))