How to compare a python datetime date with a Mysql column date? - python

Environment:
Windows 10
Python 3.7
Mysql 1:10.1.44-0ubuntu0.18.04.1
Problem:
I need to count the number of rows between 2 dates from a Mysql table. The 2 dates are made with python code. And the column date in my MySQL table is type 'text' and values look like this: "10/06/2020 18:50:17"
now = datetime.datetime.now()
previous_week_date= now - datetime.timedelta(days=7)
print(now)
print(previous_week_date)
sql_query = f"SELECT COUNT(*) FROM W551je5v_phonebot_actions WHERE id_client={id_client} AND \
platform='myplatform' AND type_action='message_sent' AND date BETWEEN CAST ('{previous_week_date}' AS DATE) AND CAST ('{now}' AS DATE)"
mycursor.execute (sql_query )
result= mycursor.fetchone ()
I get this error output:
2020-08-21 19:34:50.990393
2020-08-14 19:34:50.990393
mysql.connector.errors.ProgrammingError: 1584 (42000): Incorrect parameters in the call to stored function 'CAST'
So I guess I need an MYSQL function which will convert the value of Mysql column date text in date format, but I have no idea how to do that. The answers I found here and in Google don't respond to my need.

Related

How to select dates in SQLite with python

Im trying to query a table, and need to grab all products that have a date = today date.
Below is my code so far
import sqlite3
from datetime import date
date = date.today()
con = sqlite3.connect('test.db')
cur = con.cursor()
date = date.today()
sql_q = f'''SELECT date, name FROM table WHERE date = {date}'''
table = cur.execute(sql_q)
for row in table:
print(row)
i am using an SQlite 3 db and all data has been entered with the following format:
2022-09-20
However this variable type does not seem to work with SQL.
i know the SQL code should look somthing like this
SELECT name FROM test WHERE date = '2022-09-20'
but i'd like the date to be selected automatically from python rather than typing it in manually.
Use the function date() to get the current date:
SELECT name FROM test WHERE date = date()
or CURRENT_DATE:
SELECT name FROM test WHERE date = CURRENT_DATE
I think you need to convert date to string and then pass it in query.
maybe your datatype of column and date.today() is different.
date = date.strftime('%Y-%m-%d')
try using this.

How to query date values from sql using python where I only have the year and month

I have these records in my database:
and I want to select them using python, however I only have the year and month which is stored in a variable:
Here is my code:
However this does not work, and I've only gotten it to work when using a fully specific date, e.g. 2022-06-11.
The values of the column Date in the table are strings in the proper format yyyy-MM-dd and you can use the operator LIKE:
c.execute("SELECT Category, Amount FROM transactions WHERE Date LIKE ? || '%'", (month_selected,))
or, with the function strftime():
c.execute("SELECT Category, Amount FROM transactions WHERE strftime('%Y-%m', Date) = ?", (month_selected,))

Delete datetime from SQL database based on hour

I'm a python dev, I'm handling an SQL database through sqlite3 and I need to perform a certain SQL query to delete data.
I have tables which contain datetime objects as keys.
I want to keep only one row per hour (the last record for that specific time) and delete the rest.
I also need this to only happen on data older than 1 week.
Here's my attempt:
import sqlite3
c= db.cursor()
c.execute('''DELETE FROM TICKER_AAPL WHERE time < 2022-07-11 AND time NOT IN
( SELECT * FROM
(SELECT min(time) FROM TICKER_AAPL GROUP BY hour(time)) AS temp_tab);''')
Here's a screenshot of the table itself:
First change the format of your dates from yyyyMMdd ... to yyyy-MM-dd ..., because this is the only valid text date format for SQLite.
Then use the function strftime() in your query to get the hour of each value in the column time:
DELETE FROM TICKER_AAPL
WHERE time < date(CURRENT_DATE, '-7 day')
AND time NOT IN (SELECT MAX(time) FROM TICKER_AAPL GROUP BY strftime('%Y-%m-%d %H', time));

Error with SQL string: "Error while connecting to PostgreSQL operator does not exist: date = integer"

I have a Python(3) script that is supposed to run each morning. In it, I call some SQL. However I'm getting an error message:
Error while connecting to PostgreSQL operator does not exist: date = integer
The SQL is based on the concatenation of a string:
ecom_dashboard_query = """
with
days_data as (
select
s.date,
s.user_type,
s.channel_grouping,
s.device_category,
sum(s.sessions) as sessions,
count(distinct s.dimension2) as daily_users,
sum(s.transactions) as transactions,
sum(s.transaction_revenue) as revenue
from ga_flagship_ecom.sessions s
where date = """ + run.start_date + """
group by 1,2,3,4
)
insert into flagship_reporting.ecom_dashboard
select *
from days_data;
"""
Here is the full error:
09:31:25 Error while connecting to PostgreSQL operator does not exist: date = integer
09:31:25 LINE 14: where date = 2020-01-19
09:31:25 ^
09:31:25 HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
I tried wrapping run.start_date within str like so: str(run.start_date) but I received the same error message.
I suspect it may be to do with the way I concatenate the SQL query string, but I am not sure.
The query runs fine in SQL directly with a hard coded date and no concatenation:
where date = '2020-01-19'
How can I get the query string to work correctly?
It's more better to pass query params to cursor.execute method. From docs
Warning Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint.
So instead of string concatenation pass run.start_date as second argument of cursor.execute.
In your query instead of concatenation use %s:
where date = %s
group by 1,2,3,4
In your python code add second argument to execute method:
cur.execute(ecom_dashboard_query , (run.start_date,))
Your sentece is wrong:
where date = """ + run.start_date + """
try to compare a date and a string and this is not posible, you need to convert "run.start_date" to datetime and compare simply:
date_format = datetime.strptime(your_date_string, '%y-%m-%d')
and with this date converted to datetime do:
where date = date_format
Final code:
date_format = datetime.strptime(your_date_string, '%y-%m-%d')
ecom_dashboard_query = """
with
days_data as (
select
s.date,
s.user_type,
s.channel_grouping,
s.device_category,
sum(s.sessions) as sessions,
count(distinct s.dimension2) as daily_users,
sum(s.transactions) as transactions,
sum(s.transaction_revenue) as revenue
from ga_flagship_ecom.sessions s
where date = {}
group by 1,2,3,4
)
insert into flagship_reporting.ecom_dashboard
select *
from days_data;
""".format(date_format)

Comparing dates from from two different sources

I am using Tweepy API and storing tweets in a sqlite3 database. I am able to get the date and time using a function in Tweepy. it stores the value in the database as
2015-06-06 23:06:19
I then need to determine if the age of this date is greater than 30 days if so it needs to delete the row in the database.
c.execute ("DELETE FROM storedTweets WHERE tweetDate < DATE() - 30")
This is what I have so far but the query isn't deleting entries.
I'm sure this is simple, but I'm new to this. Thanks in advance
DATE()-30 does not return a date:
sqlite> select DATE();
2015-06-06
sqlite> select DATE()-30;
1985
But using the date function,
sqlite> select date('now','-30 days');
2015-05-07
Therefore,
c.execute("DELETE FROM storedTweets WHERE tweetDate < date('now','-30 days')")
Alternatively, you could compute the date in Python:
import datetime as DT
date = DT.date.today()-DT.timedelta(days=30)
c.execute("DELETE FROM storedTweets WHERE tweetDate < ?", [date])

Categories