从 mysql 数据库查询结果到列表
Querying results from mysql DB to a list
最终我想将主机输出到列表中。
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',) 具有相同的代码,但只选择了一列?
非常感谢任何帮助
您的 cursor
行结果始终是 tuple
类型,尝试:
for row in cursor:
print ("{}".format(row.host_name))
或
for host, in cursor:
print ("{}".format(host))
最终我想将主机输出到列表中。
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',) 具有相同的代码,但只选择了一列? 非常感谢任何帮助
您的 cursor
行结果始终是 tuple
类型,尝试:
for row in cursor:
print ("{}".format(row.host_name))
或
for host, in cursor:
print ("{}".format(host))