欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python中的sqlalchemy一些相关问题

程序员文章站 2022-06-18 16:19:02
插入中文报错如下: File "E:/PYcharm/workDir/学习过程/mysql模块/sqlalche模块.py", line 41, in data = session.query(User).filter_by().all() File "E:\python3\lib ......

插入中文报错如下:

File "E:/PYcharm/workDir/学习过程/mysql模块/sqlalche模块.py", line 41, in <module>
data = session.query(User).filter_by().all()
File "E:\python3\lib\site-packages\sqlalchemy\orm\query.py", line 2783, in all
return list(self)
File "E:\python3\lib\site-packages\sqlalchemy\orm\query.py", line 2934, in __iter__
self.session._autoflush()
File "E:\python3\lib\site-packages\sqlalchemy\orm\session.py", line 1434, in _autoflush
self.flush()
File "E:\python3\lib\site-packages\sqlalchemy\orm\session.py", line 2254, in flush
self._flush(objects)
File "E:\python3\lib\site-packages\sqlalchemy\orm\session.py", line 2380, in _flush
transaction.rollback(_capture_exception=True)
File "E:\python3\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "E:\python3\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "E:\python3\lib\site-packages\sqlalchemy\orm\session.py", line 2344, in _flush
flush_context.execute()
File "E:\python3\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 391, in execute
rec.execute(self)
File "E:\python3\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 556, in execute
uow
File "E:\python3\lib\site-packages\sqlalchemy\orm\persistence.py", line 181, in save_obj
mapper, table, insert)
File "E:\python3\lib\site-packages\sqlalchemy\orm\persistence.py", line 866, in _emit_insert_statements
execute(statement, params)
File "E:\python3\lib\site-packages\sqlalchemy\engine\base.py", line 948, in execute
return meth(self, multiparams, params)
File "E:\python3\lib\site-packages\sqlalchemy\sql\elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "E:\python3\lib\site-packages\sqlalchemy\engine\base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "E:\python3\lib\site-packages\sqlalchemy\engine\base.py", line 1200, in _execute_context
context)
File "E:\python3\lib\site-packages\sqlalchemy\engine\base.py", line 1416, in _handle_dbapi_exception
util.reraise(*exc_info)
File "E:\python3\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
raise value
File "E:\python3\lib\site-packages\sqlalchemy\engine\base.py", line 1193, in _execute_context
context)
File "E:\python3\lib\site-packages\sqlalchemy\engine\default.py", line 508, in do_execute
cursor.execute(statement, parameters)
File "E:\python3\lib\site-packages\pymysql\cursors.py", line 165, in execute
result = self._query(query)
File "E:\python3\lib\site-packages\pymysql\cursors.py", line 321, in _query
conn.query(q)
File "E:\python3\lib\site-packages\pymysql\connections.py", line 858, in query
sql = sql.encode(self.encoding, 'surrogateescape')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 43-44: ordinal not in range(256)

 

解决办法:

python中的sqlalchemy一些相关问题

一个警告信息:

连接数据库用的是pymysql,个人感觉是版本问题吧。。。。

python中的sqlalchemy一些相关问题

 出错信息:

python中的sqlalchemy一些相关问题

 

解决办法:

将pymysql改成mysqlconnector即可解决此问题

 

python中的sqlalchemy一些相关问题