Python Forum

Full Version: adddate interval in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using mysql I can get this to work. I
want to get all missing days between 2 dates.
The table was last updated on 4-10-2020. Today is 4-16-2020
As of today below is the correct output.

Output:
date field_id_id 2020-04-11 21 2020-04-12 21 2020-04-13 21 2020-04-14 21 2020-04-15 21 2020-04-16 21
Output:
set @i = -1; Select B.date, 21 as field_id_id from ( SELECT DATE(ADDDATE('2020-03-18', INTERVAL @i := @i + 1 DAY)) AS date FROM dummy HAVING @i < DATEDIFF('2020-04-16', '2020-03-18') )B left join ( Select date from dummy where field_id_id = 21 and date between '2020-03-18' and '2020-04-16' )C ON B.date = C.date where C.date is NULL
-- Below is the python giving me an error on "Interval %s "

 v_sql_tx = 'Select B.date, %s as field_id_id '\ ' from ( '\ ' SELECT DATE(ADDDATE(%s, INTERVAL %s:= %s + 1 DAY)) AS '\ ' date '\ ' FROM dummy '\ ' HAVING %s < DATEDIFF(%s, %s) '\ ' )B left join( '\ ' Select date '\ ' from dummy '\ ' where field_id_id = %s '\ ' and date between %s and %s '\ ' )C ON B.date = C.date '\ ' where C.date is NULL '


python version 3.6.8
Any suggestions to get this to work in python ?

TIA
Steve42
The code you've posted is just a string assignment. It wouldn't perform a sql query.

Line 3 also has a problem that there is a space after the backslash on the end, so as pasted, it would produce an error instead of completing the assignment. I'm not sure if that's in your code or a cut&paste error.
You need to learn how to use SQL for MySql find a good tutorial and run through it
search google for 'python 3 and mysql tutorial'
one example: https://www.mysqltutorial.org/python-mysql/