67

Please Help me. I'm running a simple python program that will display the data from mySQL database in a tkinter form...

from Tkinter import *
import MySQLdb

def button_click():
    root.destroy()

root = Tk()
root.geometry("600x500+10+10")
root.title("Ariba")

myContainer = Frame(root)
myContainer.pack(side=TOP, expand=YES, fill=BOTH)

db = MySQLdb.connect ("localhost","root","","chocoholics")
s = "Select * from member"
cursor = db.cursor()
cursor.execute(s)
rows = cursor.fetchall()

x = rows[1][1] + " " + rows[1][2]
myLabel1 = Label(myContainer, text = x)
y = rows[2][1] + " " + rows[2][2]
myLabel2 = Label(myContainer, text = y)
btn = Button(myContainer, text = "Quit", command=button_click, height=1, width=6)

myLabel1.pack(side=TOP, expand=NO, fill=BOTH)
myLabel2.pack(side=TOP, expand=NO, fill=BOTH)
btn.pack(side=TOP, expand=YES, fill=NONE)

Thats the whole program....

The error was

x = rows[1][1] + " " + rows[1][2]
IndexError: tuple index out of range

y = rows[2][1] + " " + rows[2][2]
IndexError: tuple index out of range

Can anyone help me??? im new in python.

Thank you so much....

5
  • 8
    It's saying that the index (position) you are accessing doesn't exist. Commented Nov 30, 2013 at 3:34
  • what code will i replace to my existing code??? Thank you Commented Nov 30, 2013 at 3:43
  • 6
    I don't know; could you provide a small self-contained reproducible example and edit your question to include that. Commented Nov 30, 2013 at 3:46
  • 1
    You should try to print(rows) and print(rows[1]) to see what your data looks like. It may help you finding your problem. Commented Nov 30, 2013 at 3:54
  • was getting this error when i changed train_sizes=np.linspace(0.1, 1.0, 10) to just train_sizes=10 Commented Feb 20, 2020 at 22:21

4 Answers 4

69

Probably one of the indices is wrong, either the inner one or the outer one.

I suspect you meant to say [0] where you said [1], and [1] where you said [2]. Indices are 0-based in Python.

Sign up to request clarification or add additional context in comments.

6 Comments

I was getting this message because I was passing an array to a function that was expecting a variadic sequence of arguments (eg '{}{}'.format([1,2]) vs '{}{}'.format(*[1,2])
another situation was accidentally having two '{}' inside the string, while only one variable/value in the .format()
If you have a misplaced assignment-operator (=) in an argument-list, that's another cause for this one. I had shuffled some code around and wound up with that.
...and as a variant to Dror's comment: Having {} inside a format string instead of {{}}. This is a typical copy&paste error when someone wants to literally print a pair of curly braces.
I am also getting the same error, can you check stackoverflow.com/questions/46945860/…
|
5

A tuple consists of a number of values separated by commas. like

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345

tuple are index based (and also immutable) in Python.

Here in this case x = rows[1][1] + " " + rows[1][2] have only two index 0, 1 available but you are trying to access the 3rd index.

Comments

0

I received the same error with
query = "INSERT INTO table(field1, field2,...) VALUES (%s,%s,...)"
but in the statement
cursor.execute(query, (field1, field2,..)
I had delivered less variables as necessary...
In this case I used

import mysql.connector as mysql 

I just wanted to say that this is also possible...not only in arrays
(I didn't have a very close look at this specific case...)

Comments

-1

This is because your row variable/tuple does not contain any value for that index. You can try printing the whole list like print(row) and check how many indexes there exists.

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.