gpt4 book ai didi

python - 为什么在向数据库添加数据时出现管道损坏错误?

转载 作者:太空狗 更新时间:2023-10-30 03:05:38 25 4
gpt4 key购买 nike

不确定是否有更好的方法,但我的网站上有一个注册页面,在用户注册后我添加他们的初始数据(__init__ 数据模型中的内容)然后我开始在同一部分添加一些其他信息,这给了我一个 broken pipe 错误。奇怪的是,代码似乎有效,因为我期望的条目在数据库中。我试过移动 .flush() 命令以查看它是否有帮助,但它似乎没有帮助。

Traceback (most recent call last):
File "/Users/me/Dropbox/code/eclipseWorkSpace/website/pyramidwiki/lib/python2.7/site-packages/waitress-0.8.1-py2.7.egg/waitress/channel.py", line 134, in handle_write
flush()
File "/Users/me/Dropbox/code/eclipseWorkSpace/website/pyramidwiki/lib/python2.7/site-packages/waitress-0.8.1-py2.7.egg/waitress/channel.py", line 249, in _flush_some
num_sent = self.send(chunk)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py", line 365, in send
result = self.socket.send(data)
error: [Errno 32] Broken pipe

这是我的代码:

if 'form.submitted' in request.params:
firstname = request.params['firstname']
lastname = request.params['lastname']
email = request.params['email']
password = request.params['password']
try:
new_user = Users(email, firstname, lastname, password)
DBSession.add(new_user)
#DBSession.flush() #commit so we get error if any
#add some other info
user_data = DBSession.query(Users).filter(Users.email==email).first()
user_data.join_date = datetime.datetime.now()
#create random number for verification url
user_data.vertified = id_generator(50)

DBSession.flush() #doesn't seem to make a difference where the flush is
return HTTPFound(location = request.route_url('new'))

有什么想法吗?

最佳答案

这可能不会直接回答你的问题,但“你做错了”(tm) :)

将用户对象添加到 session 后,您不需要重新查询它 - 而且,如果不首先执行 session.flush() 就尝试从数据库中查询它,将会导致错误,因为没有记录数据库呢。我会做这样的事情:

if 'form.submitted' in request.params:
firstname = request.params['firstname']
lastname = request.params['lastname']
email = request.params['email']
password = request.params['password']
try:
new_user = Users(email, firstname, lastname, password)
new_user.join_date = datetime.datetime.now()
new_user.verified = id_generator(50)

DBSession.add(new_user)
DBSession.flush() # should fail if user email is in the database
return HTTPFound(location = request.route_url('new'))

此外,您还需要检查所有执行分支(即 except: 子句、else: "if 'form.submitted' in request.params"的子句返回有意义的内容 - 您可能会得到异常,因为您的view 函数在某些情况下返回 None。实际上,这可能就是正在发生的事情——“user_data = DBSession.query(Users)”行引发异常,而 except: 部分没有返回任何内容

关于python - 为什么在向数据库添加数据时出现管道损坏错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com