0
從數據庫記錄的項目這是我的代碼:獲取在Python
import pymysql
def connect():
print("connect to database")
pw = input("Password: ")
conn = pymysql.connect(host='localhost', port=3306,
user='root', passwd=pw, db='contacts')
conn.autocommit(True)
cur_ = conn.cursor()
return cur_
def show_tables(self):
print("show tables: ")
self.execute("""SHOW TABLES""")
print(self.fetchall())
return self.fetchall()
db = connect()
table_names = show_tables(db) # returns a tuple
print(len(table_names)) # output zero
table_name = table_names[0][0] # ? - todo - get item from tuple
show_tables()
的價值迴歸(('person',),)
。 我想通過table_names[0][0]
得到名稱person
。但這不起作用。 (('person',),)
的長度也是0.但爲什麼?
編輯: 我得到的錯誤:
Traceback (most recent call last):
File "/home/kame/Dropbox/code/python/scripts/database.py", line 65, in <module>
table_name = table_names[0][0] # ? - todo - get item from tuple
IndexError: tuple index out of range
你說 「這行不通」。究竟發生了什麼? – user2357112
[此答案](http://stackoverflow.com/a/8363828/771848)有幫助嗎? – alecxe
如果我做'p =(('person',),)',然後'len(p)',我會得到預期的'1'。 'p [0] [0]'也給出了'人'(再次,如預期的那樣)。你確定你正在返回你認爲你要返回的元組嗎? – mgilson