[英]How can I take a single column from a SQL query and add it to a list in Python
I have a SQL query that will search a database and return a single row containing at least 3 different columns as follows: 我有一个SQL查询,它将搜索数据库并返回包含至少3个不同列的单行,如下所示:
SELECT
`epg_num` AS `epgNum`,
`service_key` AS `serviceKey`,
`service_name` AS `channelName`
FROM `ssr_services`
WHERE `epg_num` = %(channelValue)s
I only require the Service Key to added to a list in my code. 我只需要将服务密钥添加到代码中的列表中即可。
The above SQL query cannot be amended as it is used by other things and I would prefer not to write a new query just for this one function. 上面的SQL查询不能被修改,因为它被其他东西使用,我宁愿不要只为这个函数编写一个新的查询。 The above query is also a function in another module that I am calling to try and extract the Service Key.
上面的查询也是另一个模块中的一个函数,我正在调用该模块尝试提取服务密钥。
The user will specify the epg_num when running the code. 用户在运行代码时将指定epg_num。 Which will then bring back a row in SQL showing the epg_num, service key and service name
然后,它将在SQL中返回一行,显示epg_num,服务密钥和服务名称
Imports: 进口:
import sys
import os
import MySQLdb
from subprocess import check_output, CalledProcessError
from collections import OrderedDict
from UPnP.core.proxy import ServerProxy
from Data_Types import PlannerExportEvent, PlannerExportResponse, UpnpError, Result, Channel, Event, Shelf
from Database_Connection import FetchAll, FetchOne
from Decorators import ResetRetry, Suppress
from Logs import Logging
from Utilities import GetRecycID, GetRackID, Wait
The code I have at the moment is as follows: 我目前拥有的代码如下:
if epgNumsList:
serviceKeysList = self.GetMultiChannelInfo(epgNumsList=epgNumsList)
print(serviceKeysList)
whereString = "(epg_num IN ({epgNumsList}) AND epg_num LIKE '___')".format(epgNumsList=','.join(epgNumsList))
Which uses the following: 使用以下内容:
def GetMultiChannelInfo(self, epgNumsList=None):
serviceKeys = []
for epgNum in epgNumsList:
retObj = self.GetChannelInfo(epgNum=epgNum)
result = retObj.Result()
if result:
channelObj = retObj.Data()
result = channelObj.serviceKey
serviceKeys.append(result)
print(result)
else:
print('Channel not found')
print(serviceKeys)
return Result(serviceKeys)
I am seeing the following error: 我看到以下错误:
Traceback (most recent call last):
File "C:\Users\MHE36\workspace\Scripts\SkyPlus__UPnP_Set_Recordings.py", line 42, in <module>
upnp.BackgroundRecordings(recordings, epgNumsList, serviceKeysList, duration)
File "C:\Users\MHE36\workspace\Libraries\SuperPlanner.py", line 1154, in BackgroundRecordings
print(serviceKeysList)
File "C:\Users\MHE36\workspace\Libraries\Data_Types.py", line 32, in __repr__
return 'Result (Result: {result}, Data: {data}, NumFailures: {numFailures})'.format(result=self.result, numFailures=self.numFailures)
KeyError: 'data'
I think the trace back related to this bit of code from another module: 我认为回溯与另一个模块的这段代码有关:
def __repr__(self):
return 'Result (Result: {result}, Data: {data}, NumFailures: {numFailures})'.format(result=self.result, numFailures=self.numFailures)
Hopefully this gives enough detail but happy to provide more if needed. 希望这能提供足够的细节,但很高兴在需要时提供更多细节。
Thanks in advance. 提前致谢。
How are you executing that query? 您如何执行该查询? with pyodbc?
与pyodbc? then you can actually turn that particular row in to a dictionary
那么您实际上可以将特定行转到字典中
columns = [column[0] for column in cursor.description]
list_of_dict =[]
for dict in cursor.fetchall():
list_of_dict.append(dict(zip(columns, dict)))
print that list_of_dict and you'll understand everything. 打印该list_of_dict,您将了解所有内容。
Note : you should not execute other queries before performing that code block because cursor will be refreshed. 注意:在执行该代码块之前,请勿执行其他查询,因为将刷新游标。
What I suggest is you is that you access that list and return what you want at the time you are executing that query for some reason. 我的建议是,您出于某种原因访问该列表并在执行该查询时返回所需的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.