Date matching not working on date and object? - python

I have a variable which holds a date inputted by the user and converts it to date format using this code:
correct_date = "2022-06-08"
correct_date = dt.datetime.strptime(correct_date,'%Y-%m-%d').date()
I also have some embedded SQL in the same script that returns dates in YYYY-MM-DD format; these are saved into a dataframe:
actual_dates = pd.read_sql_query(
sql = f"""
SELECT DATE(CONTACTDATETIME) AS CONTACT_DATE
FROM TABLE1
GROUP BY DATE(CONTACTDATETIME);
""",
con = connection)
If work carried out elsewhere was done correctly, there should only be one date in the results from the SQL, which should match the date that was entered into the correct_date variable.
What I want to do is check whether this is the case. I have the code below to do this, but the problem is it always returns FAIL even when the only value in actual_dates matches correct_date.
if actual_dates["contact_date"].any() != correct_date:
print("FAIL")
else:
print("SUCCESS")
Does anyone know where I may be going wrong please? I have the suspicion it's because Python doesn't recognise a date and an object as being the same thing even when they're in YYYY-MM-DD format. Is this correct and if so does anyone know how I can work around this to achieve the required result please?

In above case, you are comparing 'String' with Date object. which will always return False.
Instead, converting the string to Date, and then comparing will give correct result.
Check below code.
if datetime.datetime.strptime(actual_dates["contact_date"],'%Y-%m-%d').date() != correct_date:
print("FAIL")
else:
print("SUCCESS")

Related

Table name with special character problem

I have a .csv file containing data of all stocks. I wanted to create a table for each stock with the table name as the stock's symbol, in a MySQL database daily_stock_recorder.
There are about 1900 items and obviously it wouldnt be feasible to write them one by one, so I created this python program, which takes the stock_symbol of each row and makes a table for it. Here's the code:
import mysql.connector as mc
import pandas as pd
mycon=mc.connect(host='localhost',user='root',passwd='1234',database='daily_stock_recorder')
cursor=mycon.cursor()
stock_df = pd.read_csv(r'C:\Users\Tirth\Desktop\pyprojects\all_stocks.csv')
for i in range(0, len(stock_df)):
stock=stock_df['SYMBOL'][i]
for j in range(0,len(stock)):
if ord(stock[j])==38:
stock_ticker=stock.replace(stock[j],'')
else:
stock_ticker=stock.replace(stock[j],stock[j])
todo="create table %s (close_price decimal(10,8) not null, traded_value bigint not null,
traded_quantity bigint not null, date_recorded date not null unique)"%(stock_ticker)
cursor.execute(todo)
mycon.commit()
As you can see I have used a 'if' statement to replace the character '&' (ASCII Value = 38) in the symbol with '' (nothing). This is because every time I run the code, it is able to create tables successfully but then gives the error when it comes to the symbol 'COX&KINGS'. I am assuming that Mysql doesnt take table names with special characters.
But even after executing with the 'if' statement for replacing the special character, I get the same name without replacement of '&' and it lands on the same error.
Can You please point out what I am doing wrong?
By the way, this is the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '&KINGS (close_price decimal(10,8) not null)' at line 1
Thanks!
If you take a small segment of your code out and run it, you'll see it doesn't do what you're expecting:
stock='A&B'
for j in range(0,len(stock)):
if ord(stock[j])==38:
stock_ticker=stock.replace(stock[j],'')
else:
stock_ticker=stock.replace(stock[j],stock[j])
print("stock_ticker is now " + stock_ticker)
This will output:
stock_ticker is now A&B
stock_ticker is now AB
stock_ticker is now A&B
As you're using the source value with each letter in itself, so it will only have the desired impact if the ticker ends with '&`
It would be much better to replace this with:
stock='A&B'
stock_ticker=stock.replace('&','')
print(stock_ticker)
That said, I would highly recommend looking into database normalization techniques. Having lots of tables named after stock tickers is bound to cause lots of problems. What if you want to track a stock that also happens to be a reserved keyword in SQL? What happens if there's a ticker A&B and AB for different stocks. It's much better to store this sort of data in one table.
The whole inner for j ... loop is unnecessary.
Replace it by
stock_ticker=stock.replace("&", "")
In the inner "j" - loop for each character stock_ticker is overwritten. If the last character isn't a & then it is overwritten by the unmodified content of stock by the
stock_ticker=stock.replace(stock[j],stock[j])

Searching a date with parameter 'LIKE'

I am building a small program in Python and SQlite where a client can type a date in the format YYYY-MM-DD and get back all the plays that take place in that specific date. All the plays are store in Play.db which saves the date as: YYYY-MM-DD HH:MM
I do not want the user to type the exact date with hour and minutes. He just has to type the year-month-day and all the plays in that specific date should appear.
I realise that I need the keyword 'LIKE' and pass my variable between '%'variable'%' in order to get a similar (not exact) match.
However I am not sure how to do so by using the syntax of my SQLite query:
user_input = input("Please, type the date of the play in format: 'YYYY-MM-DD':")
search_date = """SELECT * FROM play WHERE play_date LIKE ? """
cursor.execute(search_date, [(user_input)])
results = cursor.fetchall()
The issue is that 'results' is not populated because the SQL query is looking for exact match.
THEREFORE I tried with %user_input% or %?% but, obsiously, the variable '%user_input%' or %?% is not defined.
I understand how logically is should be done:
how to search similar strings in SQLite
But I am not sure how the "LIKE '%'variable'%'" can be obtained using my code syntax.
import datetime
user_input = datetime.datetime(input("Please, type the date of the play in format: 'YYYY-MM-DD':"), '%Y-%m-%d')
# 2011-11-11
search_date = f'SELECT * FROM play WHERE play_date LIKE {user_input}'
print(search_date)
# SELECT * FROM play WHERE play_date LIKE 2011-11-11
Is this what you're looking for?
Edited to prevent sql injections
Use the date function in your query to just compare the dates of your stored datetime values and user input.
SELECT * FROM play WHERE date(play_date) = date(?)
You need a date function. Also you can use slicing for the string. Like:
theDate = input() #example: 2018-12-24
Year = theDate[:3]
Month = theDate[5:6]
Day = theDate[8:9]
Good day,
Well it may not be exactly what you want but I would share you some pieces of code I wrote when I wanted to build something like this but this time using Flask and SQLAlchemy.
First is to convert the data to a date/datetime to ensure the data is valid
converted_date = datetime.datetime.strptime(user_input, "%Y-%m-%d").date()
Converted to regular SQL query should be
SELECT * FROM play WHERE play_date LIKE {"%" + converted_date}
Please note that the "%" is very important, and putting it at the beginning means you want to search for any date that starts with the inputted date.

How to modify the input value of a stored procedure with python

Currently, I am trying to modify two input values of the following stored procedure that I execute with Python.
country_cursor.execute(
"[DEV].[DevelopmentSchema].[QA_Extractor] #start_date='2017-05-05', #end_date='2017-05-11'")
I do not want to run this program every day and change the start_date and end_date manually from the code but instead trying to create a prompt where I can type down the dates that I want to look for the retrieval.
So far, I have done the following:
end_date = str(datetime.now()).rpartition(' ')[0]
start_date = str(datetime.now() - timedelta(days=7)).rpartition(' ')[0]
country_cursor.execute(
"[DEV].[DevelopmentSchema].[QA_Extractor] #start_date='2017-05-05', #end_date= "+"'"+end_date+"'"+"\"")
I just replaced one input date with a variable but when I execute this program I encounter the following SQL error:
pypyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server
Driver][SQL Server]An object or column name is missing or empty. For SELECT
INTO statements, verify each column has a name. For other statements, look
for empty alias names. Aliases defined as "" or [] are not allowed. Change
the alias to a valid name.')
My point of view is that the Stored Procedure does not accept this variable as the end date, in consequence, the column to look for the retrieval does not exist. I also read in SQL Server query erroring with 'An object or column name is missing or empty' which supports my view. Am I right with my thinking or am I totally wrong?
How can I fix this problem? Any ideas, suggestions and improvements are welcome ;)
If I do this:
print("[DEV].[DevelopmentSchema].[QA_Extractor] #start_date='2017-05-05', #end_date= "+"'"+end_date+"'"+"\"")
I get this:
[DEV].[DevelopmentSchema].[QA_Extractor] #start_date='2017-05-05', #end_date= '2017-05-14'"
It seems to me that there is a stray " at the end of this query string.
Part of the problem is that you are working way too hard to format the dates as strings.
I am guessing that there is
from datetime import *
at the top of your code (ugly, but hardly your fault). If so, you can do
start_date = datetime.now() - timedelta(days=7)
end_date = datetime.now()
query_string = f"[DEV].[DevelopmentSchema].[QA_Extractor] #start_date='{start_date:%Y-%m-%d}', #end_date='{end_date:%Y-%m-%d'}"
country_cursor.execute(query_string)
which arguably makes it easier to see stray punctuation.

Not a valid month error

I have a problem with python development.
I try to insert records to my database table, but I have this error when I try to insert them using cx_Oracle for python: not a valid month.
And when I try to run this same query in sqldeveloper, there is no problem.
This my code:
cursor.execute("insert into mytable(tableid,tablename,timecreate)values('45125','test',to_timestamp(to_char(sysdate,'dd/mm/yy')))")
I don't know what is the difference?
Have you any idea?
Assuming you want just the mm/dd/yy (remove hours,mins,sec) from a sysdate and convert to a timestamp, try this instead of your to_timestamp call:
CAST (trunc(SYSDATE) AS TIMESTAMP)
No need to convert to char and back.

inserting timestamps with python cassandra prepared statements

Is it possible to insert a timestamp value into a Cassandra keyspace using prepared statements of Python Cassandra driver? When I tried to do that, I got the following error message:
Expected: <class 'cassandra.cqltypes.DateType'>, Got: <type 'str'>
I see that this problem had been discussed before. But not sure whether it has been resolved. How to do this? Doing the same using simple statements would be inefficient.
Yes, you can insert a timestamp value via prepared statements by binding a datetime object. I have tried it with success.
Like Aaron said, you need to use a datetime object. Given a simple table definition:
CREATE TABLE stackoverflow2.timestamps (
bucket text,
value timestamp,
PRIMARY KEY (bucket, value)
) WITH CLUSTERING ORDER BY (value DESC)
This code will INSERT ten timestamps into the timestamps table, given a valid (connected) session:
preparedInsert = session.prepare(
"""
INSERT INTO stackoverflow2.timestamps (bucket,value) VALUES (?,?);
"""
)
#end prepare statements
for counter in range(1,10):
currentTime = datetime.datetime.today()
bucket = currentTime.strftime("%Y%m")
session.execute(preparedInsert,[bucket,currentTime])
Essentially, the datetime.datetime.today() line creates a datetime object with the current time. The strftime creates a string time bucket from it, and then the preparedInsert puts them both into Cassandra.

Categories

Resources