Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dwaynePankey opened this issue Apr 10, 2018 · 2 comments
Open

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

dwaynePankey opened this issue Apr 10, 2018 · 2 comments

Comments

@dwaynePankey
Copy link

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
Copy link
Author

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
Copy link
Author

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant