gpt4 book ai didi

python - 当 MySQL Select 语句时,Flask 出现 Python 错误

转载 作者:行者123 更新时间:2023-11-29 18:12:38 25 4
gpt4 key购买 nike

所以工作和不工作之间的区别是在 MySQLdb 的 SELECT 地址execute() 语句下面插入 SELECT 语句。

我的功能:

@app.route('/ask/', methods=['GET','POST'])
def ask():
#clients come here first
error = ''
try:
c, conn = connection()
#grab all the clients info
email = session['email']
c.execute("SELECT cid FROM clients WHERE email = (%s)", (thwart(email),))
clientcid = c.fetchone()[0]
c.execute("SELECT phone FROM clients WHERE email = (%s)", (thwart(email),))
phone = c.fetchone()[0]
c.execute("SELECT rating FROM clients WHERE email = (%s)", (thwart(email),))
rating = c.fetchone()[0]
c.execute("SELECT first_name FROM cpersonals WHERE cid = (%s)", (clientcid,))
first_name = c.fetchone()[0]
c.execute("SELECT last_name FROM cpersonals WHERE cid = (%s)", (clientcid,))
last_name = c.fetchone()[0]
c.execute("SELECT address FROM cpersonals WHERE cid = (%s)", (clientcid,))
address = c.fetchone()[0]
c.execute("SELECT zip FROM cpersonals WHERE cid = (%s)", (clientcid,))
czip = c.fetchone()[0]
c.execute("SELECT reg_date FROM cpersonals WHERE cid = (%s)", (clientcid,))
reg_date = c.fetchone()[0]
conn.commit()
c.close()
conn.close()


session['logged_in'] = 'client'
session['clientcid'] = clientcid
session['phone'] = phone
session['rating'] = rating
session['first_name'] = first_name
session['last_name'] = last_name
session['address'] = address
session['czip'] = czip
session['reg_date'] = reg_date

#if user posts a question to the pool
if request.method == 'POST':
#grab question info from form
difficulty = request.form['difficulty']
title = request.form['title']
body = request.form['body']
clientcid = session['clientcid']
c, conn = connection()

c.execute("INSERT INTO tickets (cid, difficulty, title, body) VALUES (%s, %s, %s, %s)", (clientcid, difficulty, thwart(title), thwart(body)))
conn.commit()
c.close()
conn.close()

flash("Submission successful.")
return redirect(url_for('ask'))

else:
error = "We couldn't post your question, please reload the page and try again!"

return render_template("ask.html")

except Exception as e:
return(str(e))

将其插入SELECT address FROM cpersonals下方:

c.execute("SELECT city FROM cpersonals WHERE cid = (%s)", (clientcid,))
city = c.fetchone()[0]

super 困惑,因为这与上面用于获取地址的 SELECT 语句相同,但它会导致 500 错误。有人知道我可以在哪些方面解决这个问题吗?为什么它会有这样的行为?

编辑:Apache错误日志:

[Tue Nov 14 03:20:26.932754 2017] [mpm_event:notice] [pid 10241:tid 140272413560320] AH00491: caught SIGTERM, shutting down
[Tue Nov 14 03:20:27.002887 2017] [mpm_event:notice] [pid 10347:tid 140399854022144] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.11 Python/2.7 configured -- resuming normal operations
[Tue Nov 14 03:20:27.003054 2017] [core:notice] [pid 10347:tid 140399854022144] AH00094: Command line: '/usr/sbin/apache2'
[Tue Nov 14 03:20:28.212299 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] mod_wsgi (pid=10351): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212379 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] mod_wsgi (pid=10351): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212415 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] Traceback (most recent call last):, referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212451 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212558 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] from FlaskApp import app as application, referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212622 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 41, referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212635 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] c.execute("SELECT state FROM cpersonals WHERE cid = (%s)", (clientcid,)), referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212641 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] ^, referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.212647 2017] [wsgi:error] [pid 10351:tid 140399645546240] [client 108.196.200.128:64860] SyntaxError: invalid syntax, referer: http://138.197.210.172/login/
[Tue Nov 14 03:20:28.269478 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] mod_wsgi (pid=10350): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269538 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] mod_wsgi (pid=10350): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269566 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] Traceback (most recent call last):, referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269594 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269679 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] from FlaskApp import app as application, referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269730 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 41, referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269740 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] c.execute("SELECT state FROM cpersonals WHERE cid = (%s)", (clientcid,)), referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269747 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] ^, referer: http://138.197.210.172/ask/
[Tue Nov 14 03:20:28.269753 2017] [wsgi:error] [pid 10350:tid 140399578404608] [client 108.196.200.128:64861] SyntaxError: invalid syntax, referer: http://138.197.210.172/ask/

最佳答案

想通了:我所需要做的就是将 try 语句下面的语句标记出来。我想这是因为 Python 对于制表符非常挑剔!

因此:

try:
c, conn = connection()
#grab all the clients info
email = session['email']
c.execute("SELECT cid FROM clients WHERE email = (%s)", (thwart(email),))
clientcid = c.fetchone()[0]
c.execute("SELECT phone FROM clients WHERE email = (%s)", (thwart(email),))
phone = c.fetchone()[0]
c.execute("SELECT rating FROM clients WHERE email = (%s)", (thwart(email),))
rating = c.fetchone()[0]
c.execute("SELECT first_name FROM cpersonals WHERE cid = (%s)", (clientcid,))
first_name = c.fetchone()[0]
c.execute("SELECT last_name FROM cpersonals WHERE cid = (%s)", (clientcid,))
last_name = c.fetchone()[0]
c.execute("SELECT address FROM cpersonals WHERE cid = (%s)", (clientcid,))
address = c.fetchone()[0]
c.execute("SELECT city FROM cpersonals WHERE cid = (%s)", (clientcid,))
city = c.fetchone()[0]
c.execute("SELECT state FROM cpersonals WHERE cid = (%s)", (clientcid,))
state = c.fetchone()[0]
c.execute("SELECT zip FROM cpersonals WHERE cid = (%s)", (clientcid,))
czip = c.fetchone()[0]
c.execute("SELECT reg_date FROM cpersonals WHERE cid = (%s)", (clientcid,))
reg_date = c.fetchone()[0]
conn.commit()
c.close()
conn.close()


session['logged_in'] = 'client'
session['clientcid'] = clientcid
session['phone'] = phone
session['rating'] = rating
session['first_name'] = first_name
session['last_name'] = last_name
session['address'] = address
session['city'] = city
session['state'] = state
session['czip'] = czip
session['reg_date'] = reg_date

#if user posts a question to the pool
if request.method == 'POST':
#grab question info from form
difficulty = request.form['difficulty']
title = request.form['title']
body = request.form['body']
clientcid = session['clientcid']
c, conn = connection()

关于python - 当 MySQL Select 语句时,Flask 出现 Python 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275391/

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