Skip to main content
Question

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


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

2 replies

mygis
Supporter
Forum|alt.badge.img+13
  • Supporter
  • March 12, 2016

Hi Lilly,

 

Why don't you use the timestamper transformer instead?

Forum|alt.badge.img+2
  • 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 = NoneNone
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()
    

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings