Radarspotting

Mode-S Software => Basestation => Topic started by: Pete on March 18, 2023, 02:05:44 PM

Title: Another BaseStation SQLite Question
Post by: Pete on March 18, 2023, 02:05:44 PM
Hi All. 

A big thanks to everyone who helped me with my last SQLite problem.  I'm still tinkering with my BaseStation database and have hit another couple of stumbling blocks. 

Firstly, if an aircraft is 'interested' it is represented with a 'tick' in the database.  If I want to create a script that includes if an aircraft is interested, how is the tick represented?

Secondly, in the FirstCreated and LastModified fields the date time is (for instance) entered as 2023-03-18 12:02.296.  If I wanted to search for a certain date, how could the minutes and seconds be represented by a wildcard so as include all entries from that date?

Any help greatly appreciated.

Pete
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 18, 2023, 03:00:14 PM
Hi

Sorry, I got the date format slightly wrong.  It should be (eg) 2023-03-18 14:24:44.908.

I hope it didn't confuse anyone - it did me!

Cheers

Pete
Title: Re: Another BaseStation SQLite Question
Post by: Anmer on March 18, 2023, 04:17:38 PM
This should find the records where the Interested checkbox has a value:

SELECT * FROM Aircraft

WHERE Interested =1


Not sure what date range you want to query but this works:

SELECT *   FROM      Aircraft
WHERE     FirstCreated BETWEEN
'2022-01-01 00:00:00' AND '2022-12-31 23:59:59';


Just flex the dates and times.
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 18, 2023, 04:39:46 PM
Hi Anmer

Thank you very much indeed.

Regards

Pete
Title: Re: Another BaseStation SQLite Question
Post by: benipaz on March 18, 2023, 05:01:50 PM
Quote from: Pete on March 18, 2023, 02:05:44 PM
If I wanted to search for a certain date, how could the minutes and seconds be represented by a wildcard so as include all entries from that date?

you can use date part only in where condition. sql engine will add time 0 to the dates automatically. for 2023-03-17 it will be

SELECT * FROM Aircraft
WHERE FirstCreated BETWEEN '2023-03-17' AND '2023-03-18'
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 19, 2023, 01:48:56 PM
Hi and thank you benipaz for your reply.

I have only tried the first part of Anmer's reply so far but I've found a strange result in that it's only picking up 64 records as Interested.  The only part of the script I've had to change is to substitute an * for the = as it was coming up with a syntax error. 

So my query now reads:  SELECT * FROM Aircraft WHERE Interested =1.  Is there any reason why this is not picking all the 'Interested' entries?

Pete
Title: Re: Another BaseStation SQLite Question
Post by: Anmer on March 19, 2023, 02:03:02 PM
What app are you using to run the SQL query?

I have a sqb file with over 68,000 aircraft records.

I set each one to have a check against Interested.

Running this sql returned every record:

SELECT * FROM Aircraft

WHERE Interested =1
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 19, 2023, 02:23:53 PM
I'm using SQLite Expert Personal.

I agree, there is no reason that I can see that this sql shouldn't work correctly.  The only problem I can think of is that there're corrupt areas in the Interested field of the database.  Having said that, I've run the sql again with the line:  WHERE Interested =0 and it picks up 41,000 Not Interested records as it should. 

Is there a formula that I could use showing that Interested is not equal to 0 rather than using 1?

Regards

Pete
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 19, 2023, 03:03:03 PM
I've just had a look on the internet and it seems <> 1 gives the result I'm after so I'll give it a go later and report back!

Title: Re: Another BaseStation SQLite Question
Post by: Anmer on March 19, 2023, 03:06:05 PM
Quote from: Pete on March 19, 2023, 02:23:53 PM
Is there a formula that I could use showing that Interested is not equal to 0 rather than using 1?

Thanks.  I'm also using SQLite xpert personal.

The interested column is populated by a checkbox.  The checkbox should either have a "tick" or not.  "Interested=1" should return all those with a tick.  Maybe some of the records are missing the checkbox?

Try this:

SELECT * FROM Aircraft

WHERE Interested = NULL


It might return records missing a checkbox?
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 19, 2023, 04:15:28 PM
No, unfortunately it didn't return any results therefore I don't think I'm missing any checkboxes.

I have now found out that both of the following sql work in isolation:

SELECT * FROM Aircraft WHERE Interested <>0

and

SELECT * FROM Aircraft
WHERE FirstCreated BETWEEN '2009-01-15 00:00:00' AND '2009-01-15 23:59:59';

My final problem is combining them together to only return Interested aircraft on 15 Jan 09 but I get a syntax error.  Hopefully one last piece of help should finally solve the problem.  I think the second part should select from something other than Aircraft but I can't work out what that should be.

Pete
Title: Re: Another BaseStation SQLite Question
Post by: Anmer on March 19, 2023, 04:36:18 PM
This works for me, no syntax error.  Your code has a semi-colon at the end?

SELECT * FROM Aircraft
WHERE FirstCreated BETWEEN '2009-01-15 00:00:00' AND '2009-01-15 23:59:59'
Title: Re: Another BaseStation SQLite Question
Post by: Pete on March 19, 2023, 04:42:13 PM
I think I've cracked it with a simple 'AND' and deleting the second 'SELECT * FROM Aircraft WHERE'.

Hopefully, that's it!

Thanks for your help. Anmer.
Title: Re: Another BaseStation SQLite Question
Post by: Faramir on March 19, 2023, 07:46:45 PM
The combination:
SELECT * FROM Aircraft
WHERE STRFTIME('%Y-%m-%d', FirstCreated) LIKE '2009-01-15%'
AND Interested <>0
Title: Re: Another BaseStation SQLite Question
Post by: DaveReid on March 20, 2023, 07:16:30 AM
SQLite does not enforce strong typing, so it's possible that you have some records with a numerical value 1 in the Interested column and some with a string value "1".

Try this:

Select Interested, count(*) from AIRCRAFT
Group by Interested;

You should only get two rows in your result.
Title: Re: Another BaseStation SQLite Question
Post by: rikgale on March 20, 2023, 08:57:34 AM
Is there a way of checking for this case and changing/forcing the "1" to be the correct format for the boolean (TBH - I am not sure if it should be a string or a number)
Title: Re: Another BaseStation SQLite Question
Post by: jakems on March 20, 2023, 10:46:40 AM
Hi

Interested is a boolean and is defined in Basestation as:

Interested boolean not null default 0,

So it should never have a value of null. As a boolean the test can be = true or = false. As Dave says SQLite does not enforce strong typing so depending on the app you are using you may find you get more consistent results using them. Please note I did say may!
Title: Re: Another BaseStation SQLite Question
Post by: Pete on April 10, 2023, 02:58:07 PM
Hi Dave

Apologies for the delayed reply as I've been away from my computer.  I've tried your suggested query and unfortunately, it returns a third row with 66 entries in it.

Is  there anyway of developing the sql script to produce a list of those 66 entries?

Regards

Pete

Title: Re: Another BaseStation SQLite Question
Post by: DaveReid on April 13, 2023, 07:31:52 AM
Probably.

What are the 3 values for Interested in your results table ?
Title: Re: Another BaseStation SQLite Question
Post by: Pete on April 13, 2023, 01:42:56 PM
Hi

The three rows have returned the following:

Rec No   Interested   count(*)
  1            (a Tick)     34439
  2            (blank)     41254
  3            (a Tick)     66

It seems (to me anyway) that the 66 entries in row 3 have had something other than a '1' entered into them.

I hope this helps.

Pete
Title: Re: Another BaseStation SQLite Question
Post by: DaveReid on April 14, 2023, 09:18:57 AM
SQLite Personal is telling us that there are two different non-zero values in the Interested column, both being interpreted as True - but it won't tell us what those values are.

Try the following (back up your SQB first):

Update Aircraft Set Interested = 1 where Interested <> 0

Then re-run the original query and report how many rows/values you get.
Title: Re: Another BaseStation SQLite Question
Post by: Anmer on April 14, 2023, 09:39:02 AM
If you use DB Browser it shows the actual values in the Interested column whereas SQLite Expert doesn't.

Using DB Browser you can sort on the Interested column which should show the records containing values other than 0 and 1.
Title: Re: Another BaseStation SQLite Question
Post by: Pete on April 18, 2023, 11:48:09 AM
Thanks to you both.  The third row has now gone completely and rows one and two display numbers I would expect to see.  The database has changed since I gave you the original figures so that's to be expected.  Thanks again.

Pete