Craver2000 Craver2000 - 11 months ago 109
MySQL Question

inserting timestamp into mariaDB table along with other variables

I am trying to add a current time stamp i.e.

now()
into my mariadb(SQL) table but running the script in terminal gives me an error.

The problematic part of this script is:

cursor.execute("INSERT INTO outdoorsensorstable (time, col2, col3, col4, col5, col6, col7) VALUES (%s, %s, %s, %s, %s, %s, %s)", (now(), '', '', data4, '', data6, ''))


(Data 4 and data 6 are variables that have been generated in my script prior to this.)

The error I get is:
TypeError: 'datetime.datetime' object is not callable


I then try to resolve this by placing
now()
into the VALUES segment like this:

cursor.execute("INSERT INTO outdoorsensorstable (time, col2, col3, col4, col5, col6, col7) VALUES (now(), %s, %s, %s, %s, %s, %s)", ('', '', '', data4, '', data6, ''))


but I get another error on running the script:

mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement


Any ideas why and how I can resolve this?

Answer Source

Your second effort is almost right; just don't pass any parameter for NOW(), because it is not a parameter as it is being hard coded:

sql = "INSERT INTO outdoorsensorstable (time, col2, col3, col4, col5, col6, col7) "
sql += "VALUES (now(), %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, ('', '', data4, '', data6, ''))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download