cursor.fetchall() 使用 MySQldb 和 python 返回额外的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14861162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:41:00  来源:igfitidea点击:

cursor.fetchall() returns extra characters using MySQldb and python

pythonmysqlmysql-python

提问by DavidJB

When I'm using python to fetch results from a SQL database I get extra charters at the beginning and end of the returned value. For example the below code returns ((56L,),) instead of 56, does anyone know how to get just the value... and what the (( ,),) actually mean...?

当我使用 python 从 SQL 数据库中获取结果时,我会在返回值的开头和结尾处获得额外的章程。例如,下面的代码返回 ((56L,),) 而不是 56,有谁知道如何获取值...以及 ((,),) 实际上是什么意思...?

hp= 56
id= 3

database = MySQLdb.connect (host="localhost", user = "root", passwd = "", db = "db")

cursor = database.cursor()

cursor.execute("UPDATE period_option SET points =%s WHERE period_option_id =%s", (hp, id))

cursor.execute("SELECT points FROM period_option WHERE period_option_id =%s", (po_id_home))
results = cursor.fetchall()
print results  

采纳答案by Hai Vu

fetchall()returns a list (really: a tuple) of tuples. Think of it as a sequence of rows, where each row is a sequence of items in the columns. If you are sure your search will return only 1 row, use fetchone(), which returns a tuple, which is simpler to unpack. Below are examples of extracting what you want from fetchall() and fetchone():

fetchall()返回一个元组列表(实际上:一个元组)。将其视为一系列行,其中每一行都是列中的一系列项目。如果您确定您的搜索将只返回 1 行,请使用 fetchone(),它返回一个元组,它更易于解包。以下是从 fetchall() 和 fetchone() 中提取所需内容的示例:

# Use fetchall():
((points,),) = cursor.fetchall()  # points = 56L

# Or, if you use fetchone():
(points,) = cursor.fetchone()     # points = 56L

回答by mechanical_meat

56Lis a long integer. "Long integers have unlimited precision." They can be used just like plain integers.

56L是一个长整数。“长整数具有无限的精度。” 它们可以像普通整数一样使用。

You can see just the long value by doing something like:

您可以通过执行以下操作来查看 long 值:

for result in results:
    print result[0]

Example REPL session:

REPL 会话示例:

>>> results = ((56L,),)
>>> for result in results:
...     print(result[0])
...
56

.fetchall()is defined in the DB-APIas returning the remaining rows "as a sequence of sequences (e.g. a list of tuples)". MySQLdb's default is to use a tuple of tuples which looks like this (( ,),)or even like this ( (1,2,3), (4,5,6) )where the individual rows of the resultset are the inner tuples -- and thus are always of the same length.

.fetchall()DB-API 中定义为“作为序列序列(例如元组列表)”返回剩余的行。MySQLdb 的默认值是使用看起来像这样(( ,),)甚至像这样的元组元组,( (1,2,3), (4,5,6) )其中结果集的各个行是内部元组——因此总是具有相同的长度。

回答by Nagesh HS

Try the following method, it will be help to convert fetchall() output into better list :

尝试以下方法,它将有助于将 fetchall() 输出转换为更好的列表:

 row = cursor.fetchall()
 print row 
 output is : [(1,), (2,), (3,), (4,)]
 num = list(sum(row, ()))
 print num
 output is : [1, 2, 3, 4]

回答by skipper21

If you are using your query to get just one value you can just take the 0th index.

如果您使用查询仅获取一个值,则可以只使用第 0 个索引。

results = cursor.fetchall()

if the result has only one value useresults[0]. It should give you the value you are looking for. Sometimes we get a huge result when we run a query, in this situation, we will have to iterate through the values and assign it to the list.

如果结果只有一个值,则使用results[0]. 它应该给你你正在寻找的价值。有时我们在运行查询时会得到一个巨大的结果,在这种情况下,我们将不得不遍历这些值并将其分配给列表。

>>> result
('58',)
>>> result[0]
'58'

When you run the query for huge items you get output something like this when you use curosr.fetchall()

当您对大项目运行查询时,当您使用 curosr.fetchall() 时,您会得到类似这样的输出

(('58',),('50',),('10'),)

Use follow code to get the data in list format

使用以下代码获取列表格式的数据

>>> results=(('58',),('50',),('10'),)
>>>[x[0] for x in results] --> code
   ['58', '50', '1']