I'm using mysqlx XDevAPI for python (NoSQL). I can't insert a date into my table.
table = my_schema.get_table("date_table")
field = "date_field"
insert = table.insert(field)
value = datetime.datetime.now()
insert.values(value)
insert.execute
I get an error:
ValueError: Expected token type 19 at pos 0 but found type 72
I'm presuming it's to do with the date/datetime format but I'm not sure how to find what tokens 19 or 72 are. If I try to insert a string or int I get the same error.
Yes. Like Rui said, Connector/Python doesn't support Python object conversion in the X DevAPI, you need to convert the datetime to a string format before inserting.
Related
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.
I have this date format returned from a db table : '2020-05-08 12:25:39.837597'
Query :
select max(dateSt) from tableX
Table format :
dateSt = Timestamp(6)
Is there a way to have only the first two digits from mm in python 3.X?
'2020-05-08 12:25:39.83'
EDIT:
The value is stored in a list, datatype is datetime for the specific value.
Didn't find a way to do it, so i just removed all the microseconds :
d = datetime.datetime.replace(microsecond=0)
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)
I looked at that link
It's weird because the query im doing is hit and miss.
It can't show the dates if the difference is only a few days
SQLAlchemy: how to filter date field?
model:
class UserCallsModel(db.Model):
id = db.Column(db.Integer, primary_key = True)
date = db.Column(db.String(90))
username = db.Column(db.String(90))
event_name = db.Column(db.String(90))
query:
users = UserCallsModel.query.filter(UserCallsModel.date.between("2016-1-1", "2016-1-20")).order_by(UserCallsModel.date.desc())
I've got 2 dates that fall within this range but is not getting queried?
I'm not familiar with MySQL, but I imagine it is the same as PG which I've included output below.
When you use the "between" method, you end up using the "BETWEEN" operator, like so...
SELECT * FROM my_table WHERE date BETWEEN '2016-1-1' AND '2016-1-20'
The problem is that the "between" operator does something different for dates versus strings. For example, if the value that it is testing is a string, it will see the arguments (the '2016-1-1' AND '2016-1-20' part) as strings.
mhildreth=# select '2016-1-5' between '2016-1-1' AND '2016-1-10';
?column?
----------
f
(1 row)
Meanwhile, if the value that it is testing is a date object, then it will implicitly convert the strings to date objects, essentially doing the following...
mhildreth=# select '2016-1-5'::date between '2016-1-1'::date AND '2016-1-10'::date;
?column?
----------
t
(1 row)
Thus, my guess is that you want to convert your "date" column to be a date type. If you must leave it a string, then you need to ensure that you are using a date format that also works when doing string comparison. Thus, you'll need 2016-01-01 rather than 2016-1-1.
I was under the impression that a string will actually be queried correctly as long as it was of a certain format. but nope I'm afraid it ain't so.
a better way of doing this if you have strings formatted like this:
"2016-1-5" is to simply convert the string date to a datetime.date object
python 3
import datetime
splitted_date = [int(number) for number in "2016-1-5".split("-")]
formatted_date = datetime.date(*splitted_date)
I'm sure it has something to do with registering custom type cast as described here. However, I'm not sure how to do that.
What I want to do is something like this:
SELECT * FROM table
and where a column is of date type, I want psycopg2 to convert it to Python string instead of datetime.
I found how to do it:
def register_New_Date():
# Cast PostgreSQL Date as Python string
# Reference:
# 1. http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.new_type
# 2. http://initd.org/psycopg/docs/advanced.html#type-casting-from-sql-to-python
# 1082 is OID for DATE type.
NewDate = psycopg2.extensions.new_type((1082,), 'DATE', psycopg2.STRING)
psycopg2.extensions.register_type(NewDate)
Then run:
register_New_Date()