Python Forum
How to check if the data is empty in a database?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if the data is empty in a database?
#8
(Aug-24-2017, 04:15 PM)nilamo Wrote:
(Aug-19-2017, 05:49 PM)chris0147 Wrote: If there is no null data above of the value 3021 in the same rows as the channel ITV then I won't do something.


"above" doesn't make sense in this context.  The table doesn't have a primary key (...apparently), so the rows are unordered.  The fact that they happen to look like they're ordered in whatever order they were inserted is an implementation detail, and isn't guaranteed to stay that way (ie: if you rebuild an index, the ordering could change).

It sort of looks like you want to do
select @row_id=row_id from {tablename} where program_id is null and channel = ? order by row_id desc limit 1;

update set program_id = ? where row_id = @row_id
But without having a sequential id, any solution would look pretty hackish.

Maybe the best solution would be to alter the table and add a unique, auto-generated, primary key?    

I'm sorry but I'm trying my best to explain what I am trying to achieved. I wanted to check if the row before the 3001 is empty or not so I could do something.

However, I have found the solution. Here is what I use to check if the row before the 3001 is empty or not:

     
    conn = database.connect(profilePath)
    cur = conn.cursor()
    cur.execute("SELECT channel, start_date FROM programs WHERE program_id=?;",(program_id,))
    data = cur.fetchone()
    cur.execute("SELECT channel FROM programs WHERE channel=? and stop_date=? ",(data[0],data[1]))
    data = cur.fetchone()
    cur.execute('SELECT start_date, stop_date, title FROM programs where program_id=?', [program_id])
    data = cur.fetchone()

    #if data:
    if data is not None:
       print "you are working on this........."
       print data
    else:
       print "there is no empty row before that data so you don't need to do anything..."
Reply


Messages In This Thread
RE: How to check if the data is empty in a database? - by chris0147 - Aug-27-2017, 09:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 445 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  how do you style data frame that has empty rows. gsaray101 0 544 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  How to detect abnormal data in big database python vanphuht91 5 1,169 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Database that can compress a column, or all data, automatically? Calab 3 1,232 May-22-2023, 03:25 AM
Last Post: Calab
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,151 Jan-28-2023, 12:53 PM
Last Post: Winfried
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,412 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Showing an empty chart, then input data via function kgall89 0 993 Jun-02-2022, 01:53 AM
Last Post: kgall89
  I need help parsing through data and creating a database using beautiful soup username369 1 1,727 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,199 Jul-02-2021, 02:19 PM
Last Post: xtc14
  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