解决sqlalchemy使用pymysql连接mysql之1366错误

0x00 错误情况

  • MySQL版本:mysql Ver 8.0.13 for Win64 on x86_64 (MySQL Community Server - GPL)
  • 报错情况如下
E:\Python\Python36\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 519")

0x01 解决方法

  • 拒绝使用pymsql,转而使用mysql-connector-python(听说mysql-connector也可以)
  • 安装 pip install mysql-connector-python
  • 修改引擎:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@localhost/database'  # 报1366错误情况
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqlconnector://root:password@localhost/database'  # 修改后情况
发布了46 篇原创文章 · 获赞 53 · 访问量 8万+
展开阅读全文

python提交commit()错误

03-15

![图片说明](https://img-ask.csdn.net/upload/201803/15/1521126207_767529.png) 报错 C:\Python33\python.exe C:/Users/test/sql2.py C:\Python33\lib\site-packages\pymysql\cursors.py:165: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 480") result = self._query(query) Traceback (most recent call last): File "C:\Python33\lib\site-packages\sqlalchemy\engine\base.py", line 1193, in _execute_context context) File "C:\Python33\lib\site-packages\sqlalchemy\engine\default.py", line 507, in do_execute cursor.execute(statement, parameters) File "C:\Python33\lib\site-packages\pymysql\cursors.py", line 165, in execute result = self._query(query) File "C:\Python33\lib\site-packages\pymysql\cursors.py", line 321, in _query conn.query(q) File "C:\Python33\lib\site-packages\pymysql\connections.py", line 860, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "C:\Python33\lib\site-packages\pymysql\connections.py", line 1061, in _read_query_result result.read() File "C:\Python33\lib\site-packages\pymysql\connections.py", line 1349, in read first_packet = self.connection._read_packet() File "C:\Python33\lib\site-packages\pymysql\connections.py", line 1018, in _read_packet packet.check_error() File "C:\Python33\lib\site-packages\pymysql\connections.py", line 384, in check_error err.raise_mysql_exception(self._data) File "C:\Python33\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.IntegrityError: (1062, "Duplicate entry '7' for key 'PRIMARY'") The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:/Users/test/sql2.py", line 25, in <module> session.commit() File "C:\Python33\lib\site-packages\sqlalchemy\orm\session.py", line 937, in commit self.transaction.commit() File "C:\Python33\lib\site-packages\sqlalchemy\orm\session.py", line 461, in commit self._prepare_impl() File "C:\Python33\lib\site-packages\sqlalchemy\orm\session.py", line 441, in _prepare_impl self.session.flush() File "C:\Python33\lib\site-packages\sqlalchemy\orm\session.py", line 2237, in flush self._flush(objects) File "C:\Python33\lib\site-packages\sqlalchemy\orm\session.py", line 2363, in _flush transaction.rollback(_capture_exception=True) File "C:\Python33\lib\site-packages\sqlalchemy\util\langhelpers.py", line 66, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File "C:\Python33\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise raise value File "C:\Python33\lib\site-packages\sqlalchemy\orm\session.py", line 2327, in _flush flush_context.execute() File "C:\Python33\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 391, in execute rec.execute(self) File "C:\Python33\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 556, in execute uow File "C:\Python33\lib\site-packages\sqlalchemy\orm\persistence.py", line 181, in save_obj mapper, table, insert) File "C:\Python33\lib\site-packages\sqlalchemy\orm\persistence.py", line 830, in _emit_insert_statements execute(statement, multiparams) File "C:\Python33\lib\site-packages\sqlalchemy\engine\base.py", line 948, in execute return meth(self, multiparams, params) File "C:\Python33\lib\site-packages\sqlalchemy\sql\elements.py", line 269, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) File "C:\Python33\lib\site-packages\sqlalchemy\engine\base.py", line 1060, in _execute_clauseelement compiled_sql, distilled_params File "C:\Python33\lib\site-packages\sqlalchemy\engine\base.py", line 1200, in _execute_context context) File "C:\Python33\lib\site-packages\sqlalchemy\engine\base.py", line 1413, in _handle_dbapi_exception exc_info File "C:\Python33\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File "C:\Python33\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise raise value.with_traceback(tb) File "C:\Python33\lib\site-packages\sqlalchemy\engine\base.py", line 1193, in _execute_context context) File "C:\Python33\lib\site-packages\sqlalchemy\engine\default.py", line 507, in do_execute cursor.execute(statement, parameters) File "C:\Python33\lib\site-packages\pymysql\cursors.py", line 165, in execute result = self._query(query) File "C:\Python33\lib\site-packages\pymysql\cursors.py", line 321, in _query conn.query(q) File "C:\Python33\lib\site-packages\pymysql\connections.py", line 860, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "C:\Python33\lib\site-packages\pymysql\connections.py", line 1061, in _read_query_result result.read() File "C:\Python33\lib\site-packages\pymysql\connections.py", line 1349, in read first_packet = self.connection._read_packet() File "C:\Python33\lib\site-packages\pymysql\connections.py", line 1018, in _read_packet packet.check_error() File "C:\Python33\lib\site-packages\pymysql\connections.py", line 384, in check_error err.raise_mysql_exception(self._data) File "C:\Python33\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception raise errorclass(errno, errval) sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry '7' for key 'PRIMARY'") [SQL: 'INSERT INTO user (id, name) VALUES (%(id)s, %(name)s)'] [parameters: {'id': '7', 'name': 'haha'}] (Background on this error at: http://sqlalche.me/e/gkpj) 进程已结束,退出代码1 虽然报错了,但数据库里还是插入了数据的 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览