sql - 如何使用带参数的插入查询?

标签 sql delphi delphi-2010

当我尝试使用此查询时,我收到一条错误消息,指出 Perameter 电子邮件不存在,我确信变量:电子邮件、login_pass、 payment_method、operateur 有效且存在。

SQLQuery2.sql.Text := 'INSERT INTO registered (email,login_pass,payment_method,operateur) VALUES (":email",":login_pass",":payment_method",":avecpuce")';
SQLQuery2.ParamByName('email').AsString := email;
SQLQuery2.ParamByName('login_pass').AsString := login_pass;
SQLQuery2.ParamByName('payment_method').AsString := payment_method;
SQLQuery2.ParamByName('avecpuce').AsString := avecpuce;
SQLQuery2.ExecSQL(true);

我尝试删除引用,但我得到

You have an error in your Sql syntax, check the manual that corresponds to your SQL server for the right syntax to use near ':email,:login_pass,:payment_method,:avecpuce)' at line 1

如何使用上面带参数的插入查询?

最佳答案

来自TSQLQuery.ExecSQL文档:

ExecDirect indicates that the query does not need to be prepared before it is executed. This parameter can be set to true if the query does not include any parameters.

所以如果代码使用

SQLQuery2.ExecSQL(true);

这意味着将不支持参数。

但是因为你使用了参数,所以就使用

SQLQuery2.ExecSQL;

并删除参数周围的引号。

关于sql - 如何使用带参数的插入查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667747/

相关文章:

sql - 从噪声字符串中提取电话号码

delphi - 如何使用 Delphi 调试 Windows 服务?

qt - Delphi XE - 使用 Qt GUI 库的 Delphi 项目

delphi - 单击 ListView 项时如何展开 ListView 的子项

mysql - 如何在 SQL 中选择具有 MAX(列值)、DISTINCT by MULTIPLE 列的行

sql - PostgreSQL 搜索条件必须输出丢失的行

sql - 如何编写高效的UPDATE-SELECT sql

delphi - 基于 key 的简单文本文件加密

delphi - 当拖动窗口或单击下拉菜单时,VCL TTimer 停止

delphi - 我使用Delphi调用DLL,但仍然出现错误