首页 > 解决方案 > 为什么我收到 TADOQuery 选择语句错误?

问题描述

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

我试图直接从 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 保持不变。

标签: sqldelphims-accessadodelphi-2010

解决方案


我想你只需要在列名周围加上大括号:

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

我还建议您使用参数来传递UserId而不是修改查询字符串。


推荐阅读