当我从数据库中检索数据时,它以以下格式返回,但是我只需要单独的文本就可以了:
[(u'milk',)]
[(7,)]
我尝试将其转换为字符串等,以便可以使用for循环遍历它并弹出不需要的字符,但没有任何效果
这是我的代码:
def retShopping(db):
item = []
quan = []
with sqlite3.connect(db) as db:
cursor = db.cursor()
cursor.execute('''SELECT quantity FROM Shopping''')
hold = str(cursor.fetchall())
quan.append(hold)
cursor.execute('''SELECT item FROM Shopping''')
hold2 = str(cursor.fetchall())
item.append(hold2)
print(item[0])
print(quan[0])
我希望能够仅以字符串'milk'
和'7'
结尾,以便能够在打印语句中使用它们。
答案 0 :(得分:0)
尝试一下:
def retShopping(db):
with sqlite3.connect(db) as db:
cursor = db.cursor()
cursor.execute('''SELECT quantity FROM Shopping''')
hold = cursor.fetchall()
print(hold[0][0])
cursor.execute('''SELECT item FROM Shopping''')
hold2 = cursor.fetchall()
print(hold2[0][0])