1
我有一个小的方法来从MySQL数据库中获取一列。它的工作原理,但它返回一个元组,如:获取只有一个MySQL列作为列表而不是元组
((1L,),(2L,),(3L,), ...)
我希望得到的只是一个列表[1L, 2L, 3L,...]
(甚至是铸造为int,而不是长期)。我知道我可以编写一个循环来手动执行,但可能会有一些更奇特的做法。
这是我的函数的代码,以防万一有一种方法可以直接将MySQL结果导入列表中,我还没有听说过!
def getFTPSentNotamIds():
selectedIds = None
try:
# Connect to database
db = MySQLdb.connect(host=Constants.database_host,
user=Constants.database_user,
passwd=Constants.database_passwd,
db=Constants.database_name)
# Create a cursor to execute queries
cursor = db.cursor()
# Get every ObjectID of those objects which are Selected=1
cursor.execute('SELECT ObjectID FROM MYTABLE WHERE Selected=1')
# Get the NOTAMS
selectedIds = cursor.fetchall()
except MySQLdb.Error, e:
print 'ERROR ' + str(e.args[0]) + ': ' + str(e.args[1])
return selectedIds
bleh,这消耗游标,前加载所有的查询数据。它确实有效,但请注意,这种行为适合您的应用程序。 – ThorSummoner 2016-04-24 06:36:53