mysql查询

mmvthczy  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(362)

我正在学习python编程,并尝试从简单的select查询开始实现最安全的mysql查询。问题是,每当我在查询中使用coma时,都会出现以下错误:

  1. cursor.execute(query)
  2. File "C:\Users\username\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 536, in execute
  3. stmt = operation.encode(self._connection.python_charset)
  4. AttributeError: 'tuple' object has no attribute 'encode'

我知道coma本身并不是问题的根源,但我尝试了许多不同的mysql语法,每次使用come,我都会遇到“attributeerror:'tuple'object has no attribute'encode'”错误。
我还尝试更改mysql数据库编码-没有任何更改。代码如下。

  1. import mysql.connector
  2. conn = mysql.connector.connect(
  3. charset='utf8',
  4. # init_command='SET NAMES UTF8',
  5. host="10.0.0.234",
  6. user="x",
  7. passwd="x>",
  8. database="x",
  9. )
  10. print(conn.is_connected())
  11. param = "test"
  12. cursor = conn.cursor()
  13. # =========== query below does work ========
  14. # query = ("SELECT * from list WHERE username LIKE '%test%'")
  15. # ============ query below does work =======
  16. # query = ("SELECT * from list HAVING username = '%s'" % param)
  17. # ============ query below doesn't work =====
  18. # query = ("SELECT * from list HAVING username = %s", (param,))
  19. # ============= query below doesn't work =====
  20. query = "SELECT * from list WHERE username = :name", {'name': param}
  21. cursor.execute(query)
  22. result = cursor.fetchall()
  23. for x in result:
  24. print(x)
  25. conn.close()

你知道我做错什么了吗?

xqk2d5yq

xqk2d5yq1#

答案有点棘手,但本质上是因为“query”变量的实际值是什么。。。
例如:

  1. # 1.
  2. query = ("SELECT * from list WHERE username LIKE '%test%'")
  3. # when you do this, query is a string variable,
  4. # NB: the parentheses are not necessary here
  5. # so when you call
  6. cursor.execute(query)
  7. # the value passed into the execute call is the string "SELECT * from list WHERE username LIKE '%test%'"
  8. # 2.
  9. query = ("SELECT * from list HAVING username = '%s'" % param)
  10. # when you do this, query is the result of a string formatting operation
  11. # This is a Python 2 form of string formatting
  12. # The discussion here probably makes it more clear:
  13. # https://stackoverflow.com/questions/13945749/string-formatting-in-python-3
  14. # it is almost the same as doing this:
  15. query = "SELECT * from list HAVING username = 'test'"
  16. # so when you call
  17. cursor.execute(query)
  18. # the value passed into the execute call is the string "SELECT * from list HAVING username = 'test'"
  19. # 3.
  20. query = ("SELECT * from list HAVING username = %s", (param,))
  21. # This operation is assigning a 2-value tuple into the query variable
  22. # The first value in the tuple is the string "SELECT * from list HAVING username = %s"
  23. # The second value in the tuple is a 1-value, with 'test' as its first value
  24. # 4.
  25. query = "SELECT * from list WHERE username = :name", {'name': param}
  26. # This is similar to #3, but the values in the tuple are instead
  27. # query[0] == "SELECT * from list WHERE username = :name"
  28. # query[1] is a dictionary: {'name': param}

上面的3和4都没有调用mysql execute 使用您期望的参数(请参阅此处的api)。您可能需要执行以下操作之一:
将查询元组解压到单独的变量中,并用它们调用函数

  1. operation, params = query # unpack the first elem into operation, and second into params
  2. cursor.execute(operation, params)

只需索引到查询元组中

  1. cursor.execute(query[0], query[1])
  2. # NB: you could also use the 'named parameters' feature in Python
  3. cursor.execute(query[0], params=query[1])

使用“解包参数列表”(splat运算符)

  1. cursor.execute(*query)
展开查看全部

相关问题