IndexError: tuple index out of range ----- Python

PythonPython 2.7Mysql Python

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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.

Solution 4 - Python

FORMAT FUNCTION

This error might occur in a format() function line. Try the code below on your machine

>>> 'This is just a string {}'.format()

Notice that this error occurs. Because the angle brackets were used, however, no argument was passed to format(). In Python terms,

> The number of arguments must match the number of curly braces or be superior.

Down below there are a couple of examples that are not considered good practice but are possible with no errors as far as Python3 goes.

>>> 'This is just a string'.format()  # No curly braces no arguments. Match in proportion.
>>> 'This is just a string {}'.format('WYSIWYG', 'J. Pinkman', 'ESR')  # The number of arguments is superior to the number of curlies( *curly braces* ).

Solution 5 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMSanzView Question on Stackoverflow
Solution 1 - PythonglglglView Answer on Stackoverflow
Solution 2 - PythonSai prateekView Answer on Stackoverflow
Solution 3 - PythonAnkit SinghView Answer on Stackoverflow
Solution 4 - PythonvictorkolisView Answer on Stackoverflow
Solution 5 - PythondelaflotaView Answer on Stackoverflow