您的位置:首页技术文章
文章详情页

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

【字号: 日期:2022-09-15 17:24:38浏览:25作者:猪猪

问题描述

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

想知道如何解决

注册路由代码

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

注册页面代码

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

报错

E:PythonPython35-32libsite-packagespymysqlcursors.py:166: Warning: (1366, 'Incorrect string value: ’xD6xD0xB9xFAxB1xEA...’ 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:PythonPython35-32libsite-packagesflaskapp.py', line 2000, in __call__ return self.wsgi_app(environ, start_response) File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1996, in wsgi_app ctx.auto_pop(error) File 'E:PythonPython35-32libsite-packagesflaskctx.py', line 387, in auto_pop self.pop(exc) File 'E:PythonPython35-32libsite-packagesflaskctx.py', line 376, in pop app_ctx.pop(exc) File 'E:PythonPython35-32libsite-packagesflaskctx.py', line 189, in pop self.app.do_teardown_appcontext(exc) File 'E:PythonPython35-32libsite-packagesflaskapp.py', line 1898, in do_teardown_appcontext func(exc) File 'E:PythonPython35-32libsite-packagesflask_sqlalchemy__init__.py', line 822, in shutdown_session self.session.commit() File 'E:PythonPython35-32libsite-packagessqlalchemyormscoping.py', line 157, in do return getattr(self.registry(), name)(*args, **kwargs) File 'E:PythonPython35-32libsite-packagessqlalchemyormsession.py', line 874, in commit self.transaction.commit() File 'E:PythonPython35-32libsite-packagessqlalchemyormsession.py', line 461, in commit self._prepare_impl() File 'E:PythonPython35-32libsite-packagessqlalchemyormsession.py', line 441, in _prepare_impl self.session.flush() File 'E:PythonPython35-32libsite-packagessqlalchemyormsession.py', line 2139, in flush self._flush(objects) File 'E:PythonPython35-32libsite-packagessqlalchemyormsession.py', line 2259, in _flush transaction.rollback(_capture_exception=True) File 'E:PythonPython35-32libsite-packagessqlalchemyutillanghelpers.py', line 60, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File 'E:PythonPython35-32libsite-packagessqlalchemyutilcompat.py', line 187, in reraise raise value File 'E:PythonPython35-32libsite-packagessqlalchemyormsession.py', line 2223, in _flush flush_context.execute() File 'E:PythonPython35-32libsite-packagessqlalchemyormunitofwork.py', line 389, in execute rec.execute(self) File 'E:PythonPython35-32libsite-packagessqlalchemyormunitofwork.py', line 548, in execute uow File 'E:PythonPython35-32libsite-packagessqlalchemyormpersistence.py', line 181, in save_obj mapper, table, insert) File 'E:PythonPython35-32libsite-packagessqlalchemyormpersistence.py', line 835, in _emit_insert_statements execute(statement, params) File 'E:PythonPython35-32libsite-packagessqlalchemyenginebase.py', line 945, in execute return meth(self, multiparams, params) File 'E:PythonPython35-32libsite-packagessqlalchemysqlelements.py', line 263, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) File 'E:PythonPython35-32libsite-packagessqlalchemyenginebase.py', line 1053, in _execute_clauseelement compiled_sql, distilled_params File 'E:PythonPython35-32libsite-packagessqlalchemyenginebase.py', line 1189, in _execute_context context) File 'E:PythonPython35-32libsite-packagessqlalchemyenginebase.py', line 1393, in _handle_dbapi_exception exc_info File 'E:PythonPython35-32libsite-packagessqlalchemyutilcompat.py', line 203, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File 'E:PythonPython35-32libsite-packagessqlalchemyutilcompat.py', line 186, in reraise raise value.with_traceback(tb) File 'E:PythonPython35-32libsite-packagessqlalchemyenginebase.py', line 1182, in _execute_context context) File 'E:PythonPython35-32libsite-packagessqlalchemyenginedefault.py', line 470, in do_execute cursor.execute(statement, parameters) File 'E:PythonPython35-32libsite-packagespymysqlcursors.py', line 166, in execute result = self._query(query) File 'E:PythonPython35-32libsite-packagespymysqlcursors.py', line 322, in _query conn.query(q) File 'E:PythonPython35-32libsite-packagespymysqlconnections.py', line 835, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File 'E:PythonPython35-32libsite-packagespymysqlconnections.py', line 1019, in _read_query_result result.read() File 'E:PythonPython35-32libsite-packagespymysqlconnections.py', line 1302, in read first_packet = self.connection._read_packet() File 'E:PythonPython35-32libsite-packagespymysqlconnections.py', line 981, in _read_packet packet.check_error() File 'E:PythonPython35-32libsite-packagespymysqlconnections.py', line 393, in check_error err.raise_mysql_exception(self._data) File 'E:PythonPython35-32libsite-packagespymysqlerr.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 -

问题解答

回答1:

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

回答2:

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?

回答3:

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

回答4:

username unique

回答5:

我在前面加了个查询判断 可以了

@app.route('/register', methods=['GET', 'POST'])def register(): form = RegisterForm() if form.validate_on_submit():user = User( username=form.username.data, password=form.password.data)if(User.query.filter_by(username=user.username).first()): flash('当前用户名已经注册!') return render_template('/register.html', form=form)else: flash('注册成功!') db.session.merge(user) return render_template('/register.html', form=form) return render_template('/register.html', form=form)

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

标签: Python 编程