2

I am having an issue with saving some data via SQL to a database. This code worked originally:

cnxn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=C:\\Users\\george\\Documents\\Homework\\Computing\\Monkey Studio\DrivingSchoolDatabase.accdb')
        cursor = cnxn.cursor()
        cursor.execute("insert into Students(Forename,Surname,Address1,Address2,PostCode,Home,Mobile,Email,License,Expiry,Medical,DOB,EyeTest) values (?,?,?,?,?,?,?,?,?,?,?,?,?)",forename,surname,address1,address2,postCode,homePhone,mobilePhone,email,license,expiry,medical,DOB,eyeTest)
        cnxn.commit()
        QtGui.QMessageBox.about(self, 'Saved','Student data saved')
        self.close()

I then added two new fields into the access database and added them to the code so I had this (the two new variables are previous and where):

cnxn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=C:\\Users\\george\\Documents\\Homework\\Computing\\Monkey Studio\DrivingSchoolDatabase.accdb')
        cursor = cnxn.cursor()
        cursor.execute("insert into Students(Forename,Surname,Address1,Address2,PostCode,Home,Mobile,Email,License,Expiry,Medical,DOB,EyeTest,Previous,Where) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",forename,surname,address1,address2,postCode,homePhone,mobilePhone,email,license,expiry,medical,DOB,eyeTest,previous,where)
        cnxn.commit()
        QtGui.QMessageBox.about(self, 'Saved','Student data saved')
        self.close()

I now get this error:

Traceback (most recent call last):
File "C:\Users\george\Documents\Homework\Computing\Monkey Studio\AddAStudent.py", line 118, in AddCreateStu
cursor.execute("insert into Students(Forename,Surname,Address1,Address2,PostCode,Home,Mobile,Email,License,Expiry,Medical,DOB,EyeTest,Previous,Where) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",forename,surname,address1,address2,postCode,homePhone,mobilePhone,email,license,expiry,medical,DOB,eyeTest,previous,where)
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver]           Syntax error in INSERT INTO statement. (-3502) (SQLExecDirectW)')

I'm probably missing something simple.

1 Answer 1

3

WHERE is a SQL keyword, which makes it a poor choice as a field name. If you can't rename the field, enclose it in square brackets in your INSERT statement.

... Previous,[Where]) values ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.