2
我在通過SQL將一些數據保存到數據庫時遇到問題。此代碼,最初的工作:訪問數據庫錯誤的SQL鏈接
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()
然後我增加了兩個新領域進入access數據庫,並將它們添加到代碼,所以我有這個(兩個新的變量是以前的,在哪裏):
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()
我現在得到這個錯誤:
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)')
我可能錯過了一些簡單的東西。
非常感謝! – MrPython 2014-12-03 12:42:41