为什么我的TADOQuery select语句出错?

时间:2019-05-22 15:48:47

标签: sql delphi ms-access ado delphi-2010

每次我尝试设置此变量的标题时,都会给我“未指定的错误”,并且我得出结论...

我尝试直接从TADOQuery设置标题,方法是将值分配给变量,然后将其加载到标题,但是无论哪种方式,我都得到未指定的错误。

    qry1.SQL.Text := 'SELECT Number of Games Owned FROM Users WHERE UserID = "' + sLoggedInUser + '";';
    qry1.Open;
    iCountGames := qry1['Number of Games Owned'];
    lblUserGamesOwned.Caption := 'Games Owned: ' + IntToStr(iCountGames); 

数据库设计:

Field Name               Data Type         Description
UserID                   Short Text        Maximum characters is 6
FirstName                Short Text        Maximum characters is 25
LastName                 Short Text        Maximum characters is 25
Cell Number              Short Text        Maximum characters is 10
Number of Games Owned    Number            Integer 

我希望代码将值加载到变量中,然后使用该变量设置TLabel的标题,但是所有发生的是弹出未指定的错误,并且TLabel保持不变。

1 个答案:

答案 0 :(得分:4)

我想您只需要在列名两边加上括号:

SELECT [Number of Games Owned] FROM Users WHERE UserID = "' + sLoggedInUser + '";';

我还建议您使用参数传递UserId,而不要浪费查询字符串。