MSAccess SQL Too few parameters. Expected 4

泄露秘密 提交于 2019-12-24 00:58:53

问题


I'm using Delphi XE2 and AnyDAC and an MSAccess db.

The table 'timea' has 5 fields:

Rec_No AutoNumber
App text
User_ID text
PW text
Comment memo

This code throws the error below. The query works just fine in Access query designer.

sql := 'INSERT INTO [timea] (App, User_ID, PW, Comment) VALUES ("zoo", "Bill", "mi7", "Liger");';
adconnection1.ExecSQL(sql);

Project PWB.exe raised exception class EMSAccessNativeException with message '[AnyDAC][Phys][ODBC][Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 4.'.


回答1:


Both SQL and Delphi are using single quotes as string boundaries. Since you want to have singe quote inside the string, you have to "escape" it using doube single quote.

For example, if you write S := 'Guns''N''Roses' the varieble S will contain string Guns'N'Roses - 12 characters, not 14.

Be careful when you are concatenating string values, since they might contain single quotes, too. The recommended way to write the query in this case is, for example:

sql := 'INSERT INTO Table (Col) VALUES (' + QuotedStr(Val) + ')';

Function QuotedStr wll take care and double all single quotes in the string. This is also essential to avoid insertion hacks.



来源:https://stackoverflow.com/questions/25354844/msaccess-sql-too-few-parameters-expected-4

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