我正在尝试从数据库获取国家/地区名称并将其粘贴到QComboBox。一切正常工作,除了那些来自db表的元组(''阿富汗'),('安哥拉')...问题是如何从元组中删除那些无用的字符。代码是:
countries = []
try:
conn = MySQLdb.connect(host='localhost', user='root', passwd='*****', db='world')
cursor = conn.cursor()
try:
cursor.execute("SELECT Name from country")
while True:
rows = cursor.fetchone()
if rows == None:
break
else:
countries.append(rows)
except:
print('Error CURSOR')
cursor.close()
conn.close()
except:
pass
for i in countries:
self.country_cbox.addItem(str(i)) # Converting to string
print(countries)
self.add_worker.show()
self.add_worker.exec_()
答案 0 :(得分:1)
您可以使用列名或元组索引删除值:
countries.append(rows.Name) # column name
# or use
rows[0] # column index