user3201916
user3201916

Reputation: 161

Getting date input from user and store it(python)

i have wrote a simple script to get inputs from user and store it in the database. I also want date input but some problems are arising. First I have used datetime.date() function and passing the date but it is throwing some error. I don't understand why?

import pyodbc
import datetime


class data:
       def __init__(self):
               self.name=raw_input( "Enter Name: ")
               self.section=raw_input( "Enter section: ")
               self.eon=datetime.date(1943,3, 13)  # This is in the requested 'YYYY-MM-DD' format.


a=data()
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=KIRAN-PC;DATABASE=testing')
cursor = cnxn.cursor()
cursor.execute("Insert into Students (Name,Section,Enrol_On) Values (?,?,?)",(a.name,a.section,a.eon))
cnxn.commit()

Error:

 Enter Name: Tim
Enter section: A

Traceback (most recent call last):
  File "C:/Python33/data2.py", line 15, in <module>
    cursor.execute("Insert into Students (Name,Section,Enrol_On) Values (?,?,?)",(a.name,a.section,a.eon))
Error: ('HYC00', '[HYC00] [Microsoft][ODBC SQL Server Driver]Optional feature not implemented (0) (SQLBindParameter)')

Upvotes: 0

Views: 688

Answers (1)

halex
halex

Reputation: 16403

Change the import from from datetime import datetime to import datetime. With your current code you import the class datetime from the module datetime and call the date method from this class with wrong parameters. But you want to call the constructor of the date class in the datetime module.


Update to answer your edit

According to https://code.google.com/p/pyodbc/issues/detail?id=204:

On Windows, you need to use DRIVER={SQL Server Native Client 10.0} for a driver that understands the ODBC date type.

Your current driver SQL Server does not understand how to pass the date datatype to your database.

You also have to pay attention that in your database the column Enrol_On is of type SQL_TYPE_DATE. See https://code.google.com/p/pyodbc/wiki/DataTypes for the mapping of python datatypes to SQL types.

Upvotes: 1

Related Questions