64

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....

MattDMo
  • 100,794
  • 21
  • 241
  • 231
MSanz
  • 651
  • 1
  • 5
  • 6

4 Answers4

66

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.

Archeologist
  • 169
  • 1
  • 11
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • 28
    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])` – amr Aug 30 '15 at 03:12
  • 15
    another situation was accidentally having two '{}' inside the string, while only one variable/value in the .format() – Dror May 02 '16 at 08:45
  • 1
    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. – Seldom 'Where's Monica' Needy Aug 03 '16 at 00:44
  • 1
    ...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. – cfi Dec 05 '16 at 15:39
  • I am also getting the same error, can you check https://stackoverflow.com/questions/46945860/getting-error-when-converting-a-pyscript-to-exe-using-py2exe – Pyd Oct 26 '17 at 04:38
  • In my case I started numbering of parameters in `print` from `1`, not from `0`. Example: `print({1}.format('Hello'))` should be `print({0}.format('Hello'))` – Artem Mostyaev May 04 '18 at 15:01
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.

Sai prateek
  • 11,842
  • 9
  • 51
  • 66
-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.

Ankit Singh
  • 922
  • 9
  • 16
-1

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...)

delaflota
  • 159
  • 1
  • 3
  • 12