我正在尝试检查从html页面中的表单传递的字符串。因此,表单将获取用户名,然后检查数据库是否已经创建。如果没有,它会继续创建它。我的错误出现在查找该用户名的逻辑部分。
注意,我已经注释了一些出现各种错误的区域:
import mysql.connector
import web
from mysql.connector import Error
import cgi, cgitb
cgitb.enable()
conn = mysql.connector.connect(host='localhost', database='database', user='root', password='root')
cursor = conn.cursor()
form = cgi.FieldStorage()
username = form.getvalue('username')
password = form.getvalue('password')
# check_existence = """
# SELECT username FROM members WHERE username = '%s'
# """
check_existence = """
SELECT username FROM members WHERE username = %s
"""
# cursor.execute(check_existence, username)
# "Wrong number of arguments during string formatting")
cursor.execute(check_existence, (username))
# ^pushes down to con.commit
# cursor.execute(check_existence, (username,))
# ^wrpmg number of arguments during string formatting
# with comma, the error is in commit, with comma, its in execute
conn.commit()
matches = cursor.rowcount()
现在错误指向conn.commit。虽然这取决于语法,但有时它指向它上面的行。 错误:
=> 203 conn.commit()
<class 'mysql.connector.errors.InternalError'>: Unread result found.
args = (-1, 'Unread result found.', None)
errno = -1
message = ''
msg = 'Unread result found.'
答案 0 :(得分:1)
在我有限的经验中,commit()仅用于保存(提交)数据库的更新。看起来您正在执行选择查询,但对结果不执行任何操作,并且错误与此相关。尝试将提交移动到最后,或者取消它。尝试对存储在游标中的结果使用/执行某些操作。我相信后者是解决方案。