Hi all

I need your help, I'm working on my python script to fetch the list of channels from the sqlite3 database.

I'm fetching 7 channels to set each channel in each label control 4010, 4011, 4012, 4013, 4014, 4015 and 4016. When I fetch the list of channels to put them in each label, I will get an error: IndexError: list index out of range

When I use this code:

CHANNELS_PER_PAGE = 7

#Pull the data from the database
channelList = list()
database_path = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db'))

if os.path.exists(database_path):
   #get the channels list
   cur.execute('SELECT channel FROM programs WHERE channel GROUP BY channel')

   for row in cur:
       channels = row[0].encode('ascii')
       channelList.append(channels)

       # set the channels text
       for index in range(0, CHANNELS_PER_PAGE):
           self.getControl(4110 + index).setLabel(channelList[index])

Here is the list of channels when I print it:

17:02:10 T:6400 NOTICE: 101 ABC FAMILY
17:02:10 T:6400 NOTICE: 102 CBS
17:02:10 T:6400 NOTICE: 103 CNN USA
17:02:10 T:6400 NOTICE: 105 ESPN USA
17:02:10 T:6400 NOTICE: 106 Fox News
17:02:10 T:6400 NOTICE: 107 Animal Planet
17:02:10 T:6400 NOTICE: 108 USA Network
17:02:10 T:6400 NOTICE: 110 SPIKE
17:02:10 T:6400 NOTICE: 111 BRAVO USA
17:02:10 T:6400 NOTICE: 112 BRAVO1
17:02:10 T:6400 NOTICE: 113 BRAVO2
17:02:10 T:6400 NOTICE: 114 BRAVO3
17:02:10 T:6400 NOTICE: 115 BRAVO4
17:02:10 T:6400 NOTICE: 116 BRAVO5
17:02:10 T:6400 NOTICE: 117 BRAVO6
17:02:10 T:6400 NOTICE: 118 BRAVO7

I want to get 7 strings to set each string in the label control like this:

4010 ABC FAMILY
4011 CNN USA
4012 ESPN USA
4013 Fox News
4014 Animal Planet
4015 USA Network
4016 SPIKE

Here is the result:

4010 ABC FAMILY

Recommended Answers

All 5 Replies

We don't know which one of at least 3 or 4 possible statements is causing the error because you did not include the error message. As a shot in the dark I would suggest that you print row or cur or both somewhere around this for() statement

   cur.execute('SELECT channel FROM programs WHERE channel GROUP BY channel')

   for row in cur:

This is a link to an SQLite tutorial but should be OK for whatever SQL engine you are using
http://zetcode.com/db/sqlitepythontutorial/

sorry, i have forgot to include the code where the error are jumping on.

Here is the line where the error are jumping on:

self.getControl(4110 + index).setLabel(channelList[index])

If channelList has less than seven entries then you will get an error on channelList[index]. You will have to print channelList and/or len(channelList) to see where the error is.

thank you for your advise.

Here is the result for channelList:

02:31:25 T:740  NOTICE: 101 ABC FAMILY
02:31:25 T:740  NOTICE: 102 CBS
02:31:25 T:740  NOTICE: 103 CNN USA
02:31:25 T:740  NOTICE: 105 ESPN USA
02:31:25 T:740  NOTICE: 106 Fox News
02:31:25 T:740  NOTICE: 107 Animal Planet
02:31:25 T:740  NOTICE: 108 USA Network
02:31:25 T:740  NOTICE: 110 SPIKE
02:31:25 T:740  NOTICE: 111 BRAVO USA
02:31:25 T:740  NOTICE: 112 BRAVO1
02:31:25 T:740  NOTICE: 113 BRAVO2
02:31:25 T:740  NOTICE: 114 BRAVO3
02:31:25 T:740  NOTICE: 115 BRAVO4
02:31:25 T:740  NOTICE: 116 BRAVO5
02:31:25 T:740  NOTICE: 117 BRAVO6
02:31:25 T:740  NOTICE: 118 BRAVO7

Result for len(channelList):

02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16
02:32:40 T:6008  NOTICE: 16

Here is the code i use to print the results:

for index in range(0, len(channelList)):
    print len(channelList)
    #self.getControl(4110 + index).setLabel(channelList[index])

That tells you nothing. You have to print it on the line before the error message on every pass through the for loop so you know where and what the error is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.