0

This is my code:

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() return the value (('person',),). I want to get the name person with table_names[0][0]. But this doesn't work. Also the length of (('person',),) is 0. But why?

Edit: I get the error:

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
6
  • You say "this doesn't work". What actually happens? Commented Apr 26, 2016 at 18:49
  • 1
    Does this answer help? Commented Apr 26, 2016 at 18:50
  • 3
    If I do p = (('person',),) and then len(p), I get 1 as expected. p[0][0] also gives 'person' (again, as expected). Are you sure you're returning the tuple you think you're returning? Commented Apr 26, 2016 at 18:51
  • I will post the whole code! Commented Apr 26, 2016 at 18:54
  • show us what table_names actually is, not what you're thinking it should be. Commented Apr 26, 2016 at 19:18

1 Answer 1

2

It looks like show_tables(self) is returning null, a list on None object, because you can only call one time cursor.fetchall().

The solution : comment the line

print(self.fetchall())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.