Python Forum
Search the data to update in a database
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search the data to update in a database
#1
Hi all,
I need some help, I want to search for the data into the database and replace for each string. I want to find for a data like 3001, I want to set the data to a empty string and find the next data 3002 to replace it with 3001. Then I want to find the next data 3003 to replace it with 3002, find the next data 3004 to replace it with 3003, find the next data 3005 to replace it with 3004 and so on until find the last data 3010 to replace it with 3009. After the 3009 data is updated in the database, the next row that come after the 3009 data where it have a empty data, I want to input the 3010 data.

def update_in_database(self):
     profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script', 'source.db'))
     conn1 = database.connect(profilePath)
     cur1 = conn1.cursor()

     for program_id in self.program_id:
         cur.execute('SELECT channel, program_id FROM programs WHERE program_id=?;', (program_id))
         data = cur.fetchone()

         if data is not None:
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['', ['3001'])
             #program_id = int(program_id) + 1
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3001', '3002'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3002', '3003'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3003', '3004'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3004', '3005'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3005', '3006'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3006', '3007'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3007', '3008'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3008', '3009'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3009', '3010'])
             cur.execute('UPDATE programs SET program_id=? WHERE program_id=?', ['3010', ''])
             con.commit()
Here is the contains for the self.program_id list:

['3001', '3011', '3021', '3031', '3041', '3051', '3061']
Here is the screenshot of the database:

[Image: ZUK6ATG.jpg]


The reason why I want to search for the data and replace each of them, so I can use the valve to find the data in the same row as the id to get the information I want.

Example: I want to find the data that start from 3001 to replace it with a empty string, then find the next data 3002 to replace it with 3001, find the next data 3003 to replace it with 3004 and so on then until to 3010 to replace it with 3009. So when the update is finish, I can use the id 3009 to search for the stop_date data to extract the data of 20161019130000 or extract to get the program title Bargain Hunt.

Can you please show me an example of how I can write in a better way than using in my own code?

Thanks in advance
Reply


Messages In This Thread
Search the data to update in a database - by chris0147 - Oct-26-2016, 05:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to detect abnormal data in big database python vanphuht91 5 1,164 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Database that can compress a column, or all data, automatically? Calab 3 1,225 May-22-2023, 03:25 AM
Last Post: Calab
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,406 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  simple html page with update data korenron 3 2,695 Nov-15-2021, 09:31 AM
Last Post: jamesaarr
  Get last row of SQL database and update Turtle 5 3,210 Oct-14-2021, 07:06 PM
Last Post: Turtle
  I need help parsing through data and creating a database using beautiful soup username369 1 1,726 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,195 Jul-02-2021, 02:19 PM
Last Post: xtc14
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,714 Dec-16-2020, 05:26 AM
Last Post: Vokofe
Photo Update database in tkinter shahulvk 3 3,193 Oct-24-2020, 04:48 PM
Last Post: shahulvk
  Looping to read data in database CEC68 1 1,739 Sep-24-2020, 08:54 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020