简体   繁体   English

如何从返回的数据库返回元组指定字符串值

[英]How to specify a string value from a returned database returned tuple

When running the following code I would like to be able to store the returned values in a variable as a string. 运行以下代码时,我希望能够将返回的值作为字符串存储在变量中。 However when I run the code it will return the first name from the database in this format 但是,当我运行代码时,它将以此格式从数据库返回第一个名称

[(u'Kiefer',)]

What would I need to change in order to just return a clean string? 为了只返回干净的字符串,我需要更改什么? I was thinking I could use strip although I have never used it on a tuple (not even sure if you can) but I was hoping there is a more elegant way. 我以为我可以使用条带虽然我从来没有在元组上使用它(甚至不确定你是否可以)但我希望有更优雅的方式。 Also I am using the sqlite3 module. 我也在使用sqlite3模块。

def search_db(self,ID,FName,LName):
    #try:
    FName +='%'
    LName += '%'
    self.cur.execute(("""SELECT FirstName FROM members WHERE FirstName LIKE '%s'\
    AND LastName LIKE '%s'""") % (FName, LName))
    value = (self.cur.fetchall())
    print(value)
    #except:
        #print('failed')


cla = Database()
cla.search_db(1,'K','D')

您需要访问列表的第一个元素,即元组,然后是元组的第一个元素,所以:

value[0][0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM