Syntax Error with Query

时间秒杀一切 提交于 2020-01-11 09:21:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!