1

I'm trying to do automatic alter table - adding column when it is necessary. The problem is that I've started to getting UnicodeDecodeError.

I don't understand why this error is raising. Why it wants to use 'ascii' charset.

I've tried to print attr.__class__ which prints unicode. I've already tried to attr.decode('utf-8') but still the same problem is raising.

LINE:

self.cur.execute("""ALTER TABLE data ADD COLUMN {} TEXT""".format(attr))

RETURNS:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u010d' in position 5: ordinal not in range(128)

I have # -*- coding: utf-8 -*- on the top of the file.

What should I do in this case? Is it possible to find out which encoding is used?

EDIT: If I rewrite the alter not to use variables, it works.

1
  • Like a lot of Unicode-related issues, this would probably either go away or at least give you a better error message if you were using Python 3. Commented Sep 10, 2015 at 10:48

1 Answer 1

1

To convert from unicode to bytes you use encode, not decode.

Alternatively, make the SQL string unicode:

self.cur.execute(u"""ALTER TABLE data ADD COLUMN {} TEXT""".format(attr))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.