OK strange, MySQLdb is attempting to run an "ascii" encoder on a string (which
is likely already a string, not a unicode). Try ensuring that MySQLdb isn't
anticipating Python unicode objects, using a connection form as:
create_engine('mysql+mysqldb://user:pass@host/mydb?charset=utf8&use_unicode=0')
also what is the exact version of MySQLdb - I'm on 1.2.3 which appears to be
the latest, but can't reproduce the error.
ms wrote:
> File "scraper.py", line 77, in run
> session.commit()
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/session.py",
> line 617, in commit
> self.transaction.commit()
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/session.py",
> line 293, in commit
> self._prepare_impl()
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/session.py",
> line 277, in _prepare_impl
> self.session.flush()
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/session.py",
> line 1493, in flush
> self._flush(objects)
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/session.py",
> line 1562, in _flush
> flush_context.execute()
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/unitofwork.py",
> line 327, in execute
> rec.execute(self)
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/unitofwork.py",
> line 471, in execute
> uow
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/orm/mapper.py",
> line 2107, in _save_obj
> execute(statement, params)
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py",
> line 1399, in execute
> params)
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py",
> line 1532, in _execute_clauseelement
> compiled_sql, distilled_params
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py",
> line 1633, in _execute_context
> context)
> File
> "/home/benjamin/test/test1/lib/python2.6/site-packages/SQLAlchemy-0.7.2-py2.6-linux-x86_64.egg/sqlalchemy/engine/default.py",
> line 325, in do_execute
> cursor.execute(statement, parameters)
> File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 158, in
> execute
> query = query % db.literal(args)
> File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 265,
> in literal
> return self.escape(o, self.encoders)
> File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 198,
> in string_literal
> return db.string_literal(obj)
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
> ordinal not in range(128)
>
>
> On 29 September 2011 13:59, Michael Bayer <[email protected]> wrote:
> Can you attach a traceback for that?
>
> Sent from my iPhone
>
> On Sep 29, 2011, at 7:45 AM, Benjamin Sims <[email protected]> wrote:
>
>> I've recently moved from SQLite to MySQL as the underlying database for my
>> application. The move is causing me various problems, principally around
>> Unicode. I *think* they were not occurring when SQLite was there, so I
>> thought I would ask if anybody on the list has had similar problems.
>>
>> My current difficultly is that when saving content (Chinese, generally) to
>> the database, I get the dreaded:
>>
>> *** UnicodeEncodeError: 'ascii' codec can't encode characters in position
>> 0-4: ordinal not in range(128)
>>
>> error and a rollback.
>>
>> I have checked that:
>>
>> - the MySQL table has a suitable encoding (CHARACTER SET utf8 COLLATE
>> utf8_general_ci;)
>> - the MySQL connection string has the charset on the end (?charset=utf8)
>> - The type for the relevant Column is Unicode (this worked previously with
>> just string, but I have changed it anyway just in case)
>> - The content is unicode at the point it gets sent to the the database
>> (again, this was a plain string previously but seemed to work)
>>
>> However, on save I get the above error from SQLAlchemy. What do I need to be
>> doing to get the content in the right format to send through?
>>
>> Thanks,
>> Ben
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sqlalchemy" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.