登录
首页 >  文章 >  python教程

Python 调用 MySQL 语句报错:TypeError: 'NoneType' object is not subscriptable,如何解决?

时间:2024-12-15 13:21:56 220浏览 收藏

积累知识,胜过积蓄金银!毕竟在文章开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《Python 调用 MySQL 语句报错:TypeError: 'NoneType' object is not subscriptable,如何解决?》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

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,如何解决?》文章吧,也可关注golang学习网公众号了解相关技术文章。

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>