Skip to main content
Question

How to write a python script to insert current date and time to the database?

  • March 11, 2016
  • 2 replies
  • 313 views

How to write a python script to insert current date and time to the database?
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

mygis
Supporter
Forum|alt.badge.img+14
  • Supporter
  • 307 replies
  • March 12, 2016

Hi Lilly,

 

Why don't you use the timestamper transformer instead?

Forum|alt.badge.img+2
  • 325 replies
  • March 13, 2016

You need to give us a bit more information and context e.g. which database. Does it really have to be Python? It's possible some Python modules may need to be installed for some databases (e.g. pyodbc / pymssql for SQLServer) and as you tagged the post with Desktop and Server you have to install on both.  

Here are some suggestions for an Oracle database:

  1. If you are just trying to Insert into a database for each feature then use a SQLExecutor transformer and specify the insert using that. (No Python)
  2. If you are using a Python shutdown script to insert a single record into a table for audit purposes e.g. when the process is complete, then there is a sample at the bottom of this post (I'm not a Python Jedi!)
  3. If you are using FME 2016 I wouldn't use suggestion 2 above. I would use a the new FeatureWriter transformer and connect a SQLExecutor to the Summary output port. If it really had to be Python you could replace the SQLExecutor with a PythonCaller.

0684Q00000ArKdmQAF.png

Example Python code:

import cx_Oracle
cursor, connection = None, None
try:
    connection = cx_Oracle.connect('demo/demo@xe')
    cursor = connection.cursor()
    cursor.execute("INSERT INTO importaudit (TABLE_NAME, UPDATED_DATE) VALUES ('DEMO', SYSDATE)")
    cursor.execute("COMMIT")
except cx_Oracle.DatabaseError as e:
    raise e
except Exception as e:
    raise e     
finally:
    if cursor != None:
        cursor.close()
    if connection != None:
        connection.close()