Python 调用 MySQL 语句报错:TypeError: 'NoneType' object is not subscriptable,如何解决?
python 调用 mysql 语句报错
有开发者在调用 mysql 语句时遇到了报错,如图所示:
traceback (most recent call last): file "/users/a/pycharmprojects/untitled1/main.py", line 20, in <module> cur.execute("select * from user where username=%s and password=%s", [username, password]) file "/usr/local/cellar/python@3.9/3.9.0_1/frameworks/python.framework/versions/3.9/lib/python3.9/site-packages/mysql/connector/cursor.py", line 677, in execute return self._handle_result(self._connection.cmd_query(stmt, raw=self._raw, file "/usr/local/cellar/python@3.9/3.9.0_1/frameworks/python.framework/versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 1001, in cmd_query result = self._send_cmd(servercmd.query, query) file "/usr/local/cellar/python@3.9/3.9.0_1/frameworks/python.framework/versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 869, in _send_cmd self._executed = conn.cmd_query(stmt) file "/usr/local/cellar/python@3.9/3.9.0_1/frameworks/python.framework/versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 1001, in cmd_query result = self._send_cmd(servercmd.query, query) file "/usr/local/cellar/python@3.9/3.9.0_1/frameworks/python.framework/versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 939, in _send_cmd self._handle_error(error) file "/usr/local/cellar/python@3.9/3.9.0_1/frameworks/python.framework/versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 928, in _handle_error if deferred is false and is_fatal: typeerror: 'nonetype' object is not subscriptable
这个问题的原因是 %s 没有加上双引号,正确的写法应该是:
cur.execute("select * from user where username="%s" and password="%s"", [username, password])
以上就是Python 调用 MySQL 语句报错:TypeError: 'NoneType' object is not subscriptable,如何解决?的详细内容,更多请关注其它相关文章!