python - Flask写的注册页面,当注册时,如果填写数据库里有的相同数据,就报错
迷茫
迷茫 2017-04-18 10:21:08
[Python讨论组]

Flask写的注册页面,当注册时,如果填写数据库里有的相同数据,就报错

想知道如何解决

注册路由代码

注册页面代码

报错

E:\Python\Python35-32\lib\site-packages\pymysql\cursors.py:166: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 479")
  result = self._query(query)
127.0.0.1 - - [21/Feb/2017 09:17:37] "POST /register HTTP/1.1" 200 -
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
  File "E:\Python\Python35-32\lib\site-packages\flask\app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "E:\Python\Python35-32\lib\site-packages\flask\app.py", line 1996, in wsgi_app
    ctx.auto_pop(error)
  File "E:\Python\Python35-32\lib\site-packages\flask\ctx.py", line 387, in auto_pop
    self.pop(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask\ctx.py", line 376, in pop
    app_ctx.pop(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask\ctx.py", line 189, in pop
    self.app.do_teardown_appcontext(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask\app.py", line 1898, in do_teardown_appcontext
    func(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 822, in shutdown_session
    self.session.commit()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\scoping.py", line 157, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 874, in commit
    self.transaction.commit()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 461, in commit
    self._prepare_impl()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 441, in _prepare_impl
    self.session.flush()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 2139, in flush
    self._flush(objects)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 2259, in _flush
    transaction.rollback(_capture_exception=True)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
    raise value
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 2223, in _flush
    flush_context.execute()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 389, in execute
    rec.execute(self)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 548, in execute
    uow
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\persistence.py", line 181, in save_obj
    mapper, table, insert)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\persistence.py", line 835, in _emit_insert_statements
    execute(statement, params)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
    context)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
    context)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\cursors.py", line 166, in execute
    result = self._query(query)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 835, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 1019, in _read_query_result
    result.read()
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 1302, in read
    first_packet = self.connection._read_packet()
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 981, in _read_packet
    packet.check_error()
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "E:\Python\Python35-32\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 '123' for key 'ix_users_username'") [SQL: 'INSERT INTO users (username, password) VALUES (%(username)s, %(password)s)'] [parameters: {'username': '123', 'password': '123'}]
127.0.0.1 - - [21/Feb/2017 09:17:42] "POST /register HTTP/1.1" 200 -
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(4)
黄舟

你应该先检查是否此用户已经注册,没注册再db.session.add()。 如果非要想更新主键或unique的记录,可以试试db.session.merge()。 其实执行的是select+update

PHPz

sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry '123' for key 'ix_users_username'") [SQL: 'INSERT INTO users (username, password) VALUES (%(username)s, %(password)s)'] [parameters: {'username': '123', 'password': '123'}]

mysql是否设置了主键id?

迷茫

ix_users_username不能是重复的,换个username试试

ringa_lee

username unique

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号