Search code examples
pythonpython-2.7xbmc

how to fetch the row without repeat


I'm working on my python script to pull the data from the sqlite3 database.

I have the list of channels in the database, but I have a problem with print the channels because I have stored 30 channels in sqlite3 database and I want to print one channel at a time without repeat it.

Used this code:

#Pull the data from the database
channelList = list()
programList = 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')
   row = cur.fetchone()

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

Result:

16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA
16:23:14 T:3280  NOTICE: 103 CNN USA

I want to make the result like this:

16:23:14 T:3280  NOTICE: 101 ABC FAMILY
16:23:14 T:3280  NOTICE: 102 CBS
16:23:14 T:3280  NOTICE: 103 CNN USA

EDIT: There are one channel which is missing:

16:47:21 T:2196  NOTICE: 102 CBS
16:47:21 T:2196  NOTICE: 103 CNN USA
16:47:21 T:2196  NOTICE: 105 ESPN USA
16:47:21 T:2196  NOTICE: 106 Fox News
16:47:21 T:2196  NOTICE: 107 Animal Planet
16:47:21 T:2196  NOTICE: 108 USA Network
16:47:21 T:2196  NOTICE: 110 SPIKE
16:47:21 T:2196  NOTICE: 111 BRAVO USA
16:47:21 T:2196  NOTICE: 112 BRAVO1
16:47:21 T:2196  NOTICE: 113 BRAVO2
16:47:21 T:2196  NOTICE: 114 BRAVO3
16:47:21 T:2196  NOTICE: 115 BRAVO4
16:47:21 T:2196  NOTICE: 116 BRAVO5
16:47:21 T:2196  NOTICE: 117 BRAVO6
16:47:21 T:2196  NOTICE: 118 BRAVO7

When I use this code:

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

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

Solution

  • You can do this by changing your SQL query in one of two ways:

    SELECT DISTINCT channel FROM programs WHERE channel
    

    Or:

    SELECT channel FROM programs WHERE channel GROUP BY channel
    

    My preference lies with the former, personally, since GROUP BY contains a lot of other functionality that's useful for organizing data. I wouldn't rely on it solely for removing duplicates, but the option is there.