从mysql数据库向lis查询结果

2024-10-02 16:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

最后,我想将主机输出到一个列表中

try:
  cnx = mysql.connector.connect(user='root', password='passwd', 
database='some_db')
  cursor = cnx.cursor()
except mysql.connector.Error as err:
  print("Something went wrong: {}".format(err))

retrieveQuery = ("SELECT host_name,product from server")
cursor.execute(retrieveQuery)
for host,prod in cursor:
print ("{},{}".format(host,prod))

结果看起来不错:[主机1,PowerEdge]

retrieveQuery = ("SELECT host_name from server")
cursor.execute(retrieveQuery)
for host in cursor:
print ("{}".format(host))

结果:(u'host1',)

为什么我看到(u’,)的代码相同,但只选择了一列? 非常感谢您的帮助


Tags: namefromformathostforexecuteconnectorserver