python - Inserting datetime into a MS SQL table using pyodbc -


i'm trying insert datetime value ms sql server table using pyodbc. if manually, like:

cursor.execute("""insert currentvalue(value1,currentdatetime)                                     values(55,'2014-06-27 16:42:48.533')""") 

i have no problem @ all, when try do:

currenttime = str(datetime.datetime.now()) cursor.execute("""insert currentvalue(value1,currentdatetime)                                      values(55,"""+ currenttime+")") 

i got error:

sql server incorrect syntax near '07' think number after date , starting time.

also tried this:

currenttime = "'"+str(datetime.datetime.now())+"'" 

and error comes up:

conversion failed when converting date and/or time character string.

remove datetime string conversion , instead use parameters:

.... cursor.execute("insert currentvalue (value1,currentdatetime) values(?,?)",                (value1, datetime.datetime.now())) .... 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -