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.
Related
my table
I got a problem when I try to use UPDATE to update my database. My table has 6 columns (datatype are str, datetime, int, int, float, and int, respectively). I write a python command as follow:
cur.execute("UPDATE DISCHARGE1 SET DataValue=200, flag=1 WHERE Station='12345' and DataDate='12/16/2021' and DataHour=1 and DataMinute=0")
then I got error:
cur.execute("UPDATE DISCHARGE1 SET DataValue=200, flag=1 WHERE Station='12345' and DataDate='12/16/2021' and DataHour=1 and DataMinute=0")
pyodbc.DataError: ('22018', '[22018] [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression. (-3030) (SQLExecDirectW)')
I checked and realized the problem is in the datatype of DataDate column. I then remove ' ' and the code run without error but nothing is updated. Despite the datatype of the DataDate column is datetime, the datatype required in "UPDATE" line is float. Somebody help me
To update your datas you need to commit with connection.commit() after your cur.execute
Documentation :
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-commit.html
Edit:
Have you tried to send the date like this
DataDate=Format('12/16/2021', "mm/dd/yyyy hh:nn:ss am/pm")
or
DataDate=DateValue('12/16/2021')
I am trying to implement a transformation in python sql on databricks, I have tried several ways but without success, I request a validation please:
%sql
SELECT aa.AccountID__c as AccountID__c_2,
aa.LastModifiedDate,
to_timestamp(aa.LastModifiedDate, "yyyy-MM-dd HH:mm:ss.SSS") as test
FROM EVENTS aa
The output is as follows:
It can be seen that the validation is not correct, but even so it is executed on the engine and returns null.
I have also tried performing a substring on the LastModifiedDate field from 1 to 19, but without success ...
The date format you provided does not agree with the date format of that column, so you got null. Having said that, for standard date formats like the format you have, there is no need to provide any date format at all. Simply using to_timestamp will give the correct results.
%sql
SELECT aa.AccountID__c as AccountID__c_2,
aa.LastModifiedDate,
to_timestamp(aa.LastModifiedDate) as test
FROM EVENTS aa
I frequently use pymysql to insert data into a MySQL server.
When inserting strings, I usually (but not every time) receive: pymysql.err.ProgrammingError: (1064, ...) when I insert a string using the code (where refers to a varchar):
cursor.execute("Insert into table (column) values (%s)", (stringVar))
Typically I have to do something like:
cursor.execute("Insert into table (column) values ('"+stringVar+"')"))
However, sometimes that throws the same error and I have to do something like:
stringVar="'"+stringVar
stringVar=stringVar+"'"
cursor.execute("Insert into table (column) values ("+stringVar+")")
This just isn't a feasible way to program this operation.
I assume I am messing up something simple but I cannot figure out what this is. I use pymysql a lot and this error is really starting to wear on me. Any help would be much appreciated!
cursor.execute('INSERT INTO table (column) VALUES (?)', (stringVar,))
Whenever you're trying to directly format a string into a query like that, it's basically always a sign you're doing something wrong. Every python database interface I'm aware of has a way to pass parameters to queries like above. Note that having the stringVar contained within an iterable is required.
I am using python driver and Cassandra, I have created the following schema
CREATE TABLE channelFollowers(
channelID BIGINT,
isfavorite BOOLEAN,
userID BIGINT,
followDate TIMESTAMP,
PRIMARY KEY (userID, channelID, followDate)
);
my question is, how can i use the followDate in where clause of select and update query, I have tried but it does not work it gives the following error
typeerror: not enough arguments for format string
can any body help me please?
Here is my code
channelLike = channelSession.execute("update channelfollowers set isblocked=%s, isfavorite=%s where userid=%s and channelid=%s and followdate=%s",[int(userid),int(channel_id),followDate]
From what I see in the error message & in your code - you have 5 placeholders in format string (%s), but pass only 3 values. I believe that you omit placeholders for isblocked & isfavorite parameters - either pass them, or replace %s with true/false values (as I can see from query, they should have boolean type)
P.S. you also don't have isblocked in the table's definition
I used pyodbc to access my MSSQL database.
When reading uniqueidentifier field from MSSQL, in my MacOS, I can print the correct value of udid field (e.g 4C444660-6003-13CE-CBD5-8478B3C9C984), however when I run the same code on Linux CentOS, i just see very strange string like "???E??6??????c", and the type of value is "buffer", not "str" as in MacOS.
Could you explain me why it is and how can i get correct value of uidi on linux? Thanks
In linux i use str(uuid.UUID(bytes_le=value)).upper() to get string like 4C444660-6003-13CE-CBD5-8478B3C9C984 of uniqueidentifier field
This is a few years old, but I've had to tackle this same problem recently. My solution was to simply CAST the unique identifier as a VARCHAR, which kept my Python code nice and tidy:
SELECT CAST(unique_id_column AS VARCHAR(36)) AS my_id FROM...
Then in Python, simply output row.my_id.