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?
#1
Hi,
I'm fetching the data from the database and I would like to check the data above of the data in the same rows as the per channel to see if the cell is empty.

Here is for example table:

---------------------------
| channel   | program_id
---------------------------
| ITV       |
| ITV       | 3021
| ITV       | 3022
| ITV       | 3023


Here is the code:

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 WHERE program_id=?;', (program_id,))
     data = cur.fetchone()


     if data:
       #check if the data above the 3021 in a database is empty
Here is the output for the data:

(u'103 ITV', u'3021')
I have got a string of program_id which it is 3021, so I want to check the string in a database to see if the data above of the 3021 is empty so I could do something.

How I can check in a database to see if the data above of the string is empty or not?
Reply
#2
you can find all empty rows with '
SELECT channel FROM programs WHERE program_id is NULL;'
Reply
#3
(Aug-19-2017, 03:58 PM)Larz60+ Wrote: you can find all empty rows with '
SELECT channel FROM programs WHERE program_id is NULL;'

Thank you but there is a problem.

When I try this:

cur.execute('SELECT channel FROM programs WHERE program_id is NULL;', (program_id,))
data = cur.fetchone()
print "data for update_in_database..................."
print data
It will give me an error: ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

Do you know why I am getting an error?

Are you sure this is the correct code I could use to check if the row above of the program_id is empty not on below?
Reply
#4
The error is in the first line. Since you changed program_id = ? to program_id is NULL, you don't need program_id in the second argument. That is, you took out where you were placing the thing (the ?), but you didn't take out what you were placing (program_id).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Aug-19-2017, 05:09 PM)ichabod801 Wrote: The error is in the first line. Since you changed program_id = ? to program_id is NULL, you don't need program_id in the second argument. That is, you took out where you were placing the thing (the ?), but you didn't take out what you were placing (program_id).

You have got it wrong there, I need to use program_id as a second argument because I am getting the value from the button object in each time when I get the value so I need to use it.

By the way, I try to use this:
cur.execute('SELECT program_id FROM programs WHERE program_id is NULL and channel=?;', (channel,))

if data is None:
   print "Now you are working on it........................."
else:
   print "You won't do anything........................"
It did not look up for the NULL data above of the value in a database in the same row as the channel when I am trying to use the channel variable to find the channel name in the same rows as the value.

If you are not sure what I am trying to do, please see the screenshots:

http://i.imgur.com/nwJGog8.png

http://i.imgur.com/hoe62gt.png

I want to find the value 3001, then look up of the null data in the same rows as the channel BBC ONE before I would do something. 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. I hope you get my point?
Reply
#6
(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?
Reply
#7
OT: What database is this? Is it a db written in Python and if so what?

Thanks.
Reply
#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
#9
Chris,
It has been some time since I've done database work and time has not permitted me to read this entire thread... but if you could answer a few questions...
1 - What kind of database are you trying to query? Is it written in Python ( or an off the shelf product?
2 - Is it a flat-file (single table) or a relational db (set of tables) linked together by primary keys or what?
3 - Are you querying a single table in the db or are attempting to query multiple tables?
4 - If a single table roughly how many records (rows)? 100, 1,000, 10,000 or more?
5 - Ditto for the record set you would expect to get back from a multi-table query
6 - Considering the resultant record set a virtual table are you attempting to...
.. search for the first row that contains the "target text" in column N...
.. then examine Column N in the previous row to see if it is either null or blank?
7 - If the rows are un-ordered how can you write a query to look for the "previous" row ? (Maybe that is your question)
8 - Have you considered...
.. importing one row at at time into a simple python list variable...
.. use a Python counter keep up with what row is the current row...
.. then examining the n'th element of that list for your target...
.. and if found...
.. read in the previous row and examine the same element to see if it is null or blank.
.. if not found import the next row of the table into the list variable and inc the counter and repeat... or whatever

# Basically ( I think) you would
.. use SQL to return the ROW and Python to put the results into a "list"
.. use Python to keep up with which ROW is the current row under investigation
.. use Python to examine the n'th element of the list
.. etc

I'm probably way off here as haven't done any db work in a long time and am very new to Python... but maybe answering my questions and reading my comments will spark an idea.

Actually I'm asking this as I am interested in using Python with a database I have in mind... also just Googled and see SQLite3... is that what you are working with?
Reply


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 318 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  how do you style data frame that has empty rows. gsaray101 0 500 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  How to detect abnormal data in big database python vanphuht91 5 1,064 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  Database that can compress a column, or all data, automatically? Calab 3 1,120 May-22-2023, 03:25 AM
Last Post: Calab
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,067 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,306 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Showing an empty chart, then input data via function kgall89 0 946 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,688 Sep-22-2021, 08:45 PM
Last Post: Larz60+
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,116 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Looping to read data in database CEC68 1 1,679 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