I want to insert a date into Ms-SQL DB. How can I do that?
Here's what I'm doing:-
a = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
data = {'AWB_Number':'1','Weight':'1','Length':'1','Height':'1','Width':'1','Customer_Name':'Naaptol','Scan_Time': a,'Series_Flag':'Others'}
data = (
data['AWB_Number'], data['Weight'], data['Length'], data['Height'],
data['Width'], data['Customer_Name'], data['Scan_Time'] ,data['Series_Flag']
)
print data
con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (aramex_dsn, aramex_user, aramex_password, aramex_database)
cnxn = pyodbc.connect(con_string)
cursor = cnxn.cursor()
cursor.execute("insert into data_AutoScale_DELHUB VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" % data)
cnxn.commit()
cnxn.close()
It returns an error saying
Traceback (most recent call last):
File "tests.py", line 39, in <module>
cursor.execute("insert into data_AutoScale_DELHUB VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" % data)
pyodbc.ProgrammingError: ('42000', "[42000] [FreeTDS][SQL Server]Incorrect syntax near '09'. (102) (SQLExecDirectW)")
What is the problem?
Following is the Database structure:-
AWB_Number = models.CharField(max_length = 255)
Weight = models.CharField(max_length = 255)
Length = models.CharField(max_length = 255)
Width = models.CharField(max_length = 255)
Height = models.CharField(max_length = 255)
Customer_Name = models.CharField(max_length = 255)
Scan_Time = models.DateTimeField(db_index = True)
Series_Flag = models.CharField(max_length = 255)