最终我想将主机输出到列表中。
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))
结果看起来不错:[host1,PowerEdge]
retrieveQuery = ("SELECT host_name from server")
cursor.execute(retrieveQuery)
for host in cursor:
print ("{}".format(host))
结果:(u'host1',)
为什么我看到(u',)使用相同的代码,但只选择了一列? 非常感谢任何帮助
答案 0 :(得分:0)
您的cursor
行结果始终为tuple
类型,请尝试:
for row in cursor:
print ("{}".format(row.host_name))
或
for host, in cursor:
print ("{}".format(host))