第一周使用任何语言进行编程,需要一些帮助:我试图从sqlite3中提取一个文本值作为字符串。I‘我得到的值是一个列表而不是str:
system_id = raw_input ("What is your System ID: ")例如,用户输入“Elmer”
conn = sqlite3.connect("API.db")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS SYSTEMID (systemid TEXT)''')
conn.commit()
conn.close()
conn = sqlite3.connect("API.db")
c = conn.cursor()
system_id = c.execute('select systemid from SYSTEMID').fetchall()
conn.close()那么,如何将变量system_id作为字符串呢?如果我从这里打印出(u,Elmer),我想要system_id = Elmer
发布于 2016-12-16 15:51:16
正如Ev. Kounis所指出的,默认情况下,fetchall()将返回长度等于指定变量的tuples的list。
在您的例子中,您得到了长度为1的元组,因为您要求一个变量。
在获取字符串时,默认情况下,字符串前面的u也会返回'Elmer' (这仅仅意味着它是一个Unicode字符串)。
你应该做两件事:
u的连接的text_factor来摆脱str,如下所示:
con.text_factory = str'Elmer'fetch = c.execute('select systemid from SYSTEMID').fetchall()
system_id = fetch[0][0] # First element in first tuple in listhttps://stackoverflow.com/questions/41187746
复制相似问题