问题
I would like to use an Insert Into
query in Delphi XE2 to insert a user's information into a MS Access Database. The problem is I keep getting the same error:
Syntax error in INSERT INTO statement
I have done some research but there is no definitive answer. my source code is:
opendb('QuizDB.mdb');
DB.Close;
DB.SQL.Add('INSERT INTO tblUsers');
DB.SQL.Add('(FirstName,Surname,Username,Password,Grade)');
DB.SQL.Add('Values (:Firstname, :Surname, :Username, :Password, :Grade)');
Db.Parameters.ParamByName('Firstname').Value := pFirstname;
Db.Parameters.ParamByName('Surname').Value := pSurname;
Db.Parameters.ParamByName('Username').Value := pUsername;
Db.Parameters.ParamByName('Password').Value := pPassword;
Db.Parameters.ParamByName('Grade').Value := pGrade;
DB.ExecSQL;
QuizDB being the database name, DB being a ADOQuery component and then p(var) being variables received as parameters.
How do I make it work?
回答1:
PASSWORD
is a reserved word in Access so if you use it as a column name you must enclose it in square brackets.
Try this instead:
DB.SQL.Add('INSERT INTO tblUsers ');
DB.SQL.Add('(FirstName,Surname,Username,[Password],Grade) ');
来源:https://stackoverflow.com/questions/19255272/syntax-error-with-query