Python Forum

Full Version: Move the data up to the next row in a database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
I need some help with my code, I am using the code to write data to input them in the database where I will move the data down from the same row as the other data.

Example:

=======================================================
name           |   start_date   |    stop_date      |   program_id     |  programme_title         |
=======================================================
channel 1                                                          3001                  some name 1
channel 1                                                          3002                  some name 2
channel 1                                                          3003                  some name 3


Here is what it will move the program_id to the next row that go down:

=======================================================
name           |   start_date   |    stop_date      |   program_id     |  programme_title         |
=======================================================
channel 1                                                                                   some name
channel 1                                                          3001                  some name 2
channel 1                                                          3002                  some name 3


This is the code I use:

def update_in_database(self):
     profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db'))
     conn = database.connect(profilePath)
     cur = conn.cursor()
     program_id = ''.join(str(x) for x in self.program_id)
     cur.execute('SELECT channel, program_id FROM programs')
     data = cur.fetchone()

     if data is not None:
         value = program_id
         for i in range(0,10):
             if value == program_id:
                 cur.execute("UPDATE programs set program_id=? WHERE program_id=?",('',value))
             else:
                 cur.execute("UPDATE programs set program_id=? WHERE program_id=?",(value-1,value))
             value = int(value) + 1
         cur.execute("SELECT channel , stop_date FROM programs WHERE program_id=?;",(value-2,))
         data = cur.fetchone()
         try:
             cur.execute("UPDATE programs set program_id=? WHERE channel=? and start_date=? ",(value-1,data[0],data[1]))
         except:
             pass
         conn.commit()
         conn.close()
I want to know how I can move the data up in the opposite than the code I use as I want to move the data up?

Example:

=======================================================
name           |   start_date   |    stop_date      |   program_id     |  programme_title         |
=======================================================
channel 1                                                                                   some name
channel 1                                                          3001                  some name 2
channel 1                                                          3002                  some name 3



Here is what it will move the program_id to the next row that go up:

=======================================================
name           |   start_date   |    stop_date      |   program_id     |  programme_title         |
=======================================================
channel 1                                                          3001                  some name 1
channel 1                                                          3002                  some name 2
channel 1                                                          3003                  some name 3


​​​​​​​
Do you know how I could do that?
Are they always sequential like that?  Because I think just writing sql will replace the entire python snippet: update programs set program_id=program_id-1