pythonsqltkintercalendarwidget

Compare tkinter calendar get_date() Date with datetime field in Oracle db :


I´m trying to extract data within two tkinter calendar dates from an Oracle DB, Date in calendar is format date_pattern='yyyy-mm-dd' Date in Oracle DB is datetime yyyy-mm-dd hh:mm:dd, but I keep getting errors such as:

    -TypeError:can only concatenate str (not "Timestamp") to str
     Exception in Tkinter callback
    -TypeError: can only concatenate str (not "datetime.date") to 
     str. Exception in Tkinter callback
    And the last error:
     -oracledb.exceptions.DatabaseError: ORA-01861: literal does 
      not match format string. Exception in Tkinter callback 






Fecha = self.cal1.get_date()
Fecha2=Fecha.strftime("%Y-%m-%d %H:%M:%S")
sql = " SELECT t3.DESCRIPTION as ASSET_DESCRIPTION from maximo.BTV_workorder T1 inner join maximo.BTV_PM t2 on t2.Assetnum=T1.Assetnum left join maximo.BTV_ASSET t3 on T1.ASSETNUM=t3.ASSETNUM  where  T1.istask='1' and T1.SITEID='ALBAL' and T1.STATUS='COMP' and t2.STATUS='ACTIVE' and t1.STATUSDATE ='" + Fecha2 + "'"

Solution

  • You need to use TO_DATE function of oracle and pass your fecha2 string to that function assuming your statusdate column's type is DATE. Try below.

    fecha = self.cal1.get_date()
    
    fecha2 = fecha.strftime("%Y-%m-%d %H:%M:%S")
    
    sql = f"""
        SELECT t3.DESCRIPTION AS ASSET_DESCRIPTION
        FROM maximo.BTV_workorder T1
        INNER JOIN maximo.BTV_PM t2 ON t2.Assetnum = T1.Assetnum
        LEFT JOIN maximo.BTV_ASSET t3 ON T1.ASSETNUM = t3.ASSETNUM
        WHERE T1.istask = '1'
          AND T1.SITEID = 'ALBAL'
          AND T1.STATUS = 'COMP'
          AND t2.STATUS = 'ACTIVE'
          AND T1.STATUSDATE = TO_DATE('{fecha2}', 'YYYY-MM-DD HH24:MI:SS')
    """