How to write a python script to insert current date and time to the database?
Page 1 / 1
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:
- 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)
- 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!)
- 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.
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()