
我有一个查询,我在运行时创建一些值可能为null.我不知道如何让Firebird接受明确的null值,我需要离开为null.在这个阶段我正在构建sql,以便我不包括null的参数,但这是冗长乏味且容易出错的.
var Qry: Tsqlquery;begin SetConnection(query); // sets the TsqlConnection property to a live database connection query.sql.Text := 'INSERT INTO Sometable (ThisColumn) VALUES (:ThisValue)'; query.ParamByname('ThisValue').IsNull := true; // read only,true by default query.ParamByname('ThisValue').Clear; // does not fix the problem query.ParamByname('ThisValue').IsNull = true; // still true query.ParamByname('ThisValue').Bound := true; // does not fix the problem query.Execsql; 目前一个EDatabaseError“在DB.pas中引发了参数”ThisValue“”“的值,所以我怀疑这是通过设计而不是Firebird问题.
可以将参数设置为NulL吗?如果是这样,怎么办?
(编辑:对不起,没有明确的尝试.清除之前,我留下来,赞成提到IsNull.已经添加声明和更多的代码)
对不起,还有一件事:表上没有“NOT NulL”约束.我不认为这是远远的,但是以为我应该说.
完整的控制台应用程序显示问题在我的结束:
program InsertNull;{$APPTYPE CONSolE}uses DB,sqlExpr,Variants,SysUtils;var sqlConnection1: TsqlConnection; query: Tsqlquery;begin sqlConnection1 := TsqlConnection.Create(nil); with sqlConnection1 do begin name := 'sqlConnection1'; Drivername := 'Interbase'; GetDriverFunc := 'getsqlDriverINTERBASE'; libraryname := 'dbexpint.dll'; LoginPrompt := False; Params.clear; Params.Add('Database=D:\Database\ZMDDEV12\clinplus'); Params.Add('Rolename=Rolename'); //REDACTED Params.Add('User_name='); //REDACTED Params.Add('Password='); Params.Add('ServerCharSet='); Params.Add('sqlDialect=1'); Params.Add('BlobSize=-1'); Params.Add('CommitRetain=False'); Params.Add('WaitOnLocks=True'); Params.Add('ErrorResourcefile='); Params.Add('LocaleCode=0000'); Params.Add('Interbase TransIsolation=ReadCommited'); Params.Add('Trim Char=False'); vendorlib := 'gds32.dll'; Connected := True; end; sqlConnection1.Connected; query := Tsqlquery.Create(nil); query.sqlConnection := sqlConnection1; query.sql.Text := 'INSERT INTO crs_edocument (EDOC_ID,linkAGE_TYPE) VALUES (999327,:ThisValue)'; //query.ParamByname('ThisValue').IsNull := true; // read only,true by default// query.ParamByname('ThisValue').Value := NulL; query.ParamByname('ThisValue').clear; // does not fix the problem query.ParamByname('ThisValue').Bound := True; // does not fix the problem// query.ParamByname('ThisValue').IsNull; // still true query.Execsql;end.解决方法 错误的原因是“dbx”不知道参数的数据类型.由于从未分配值,因此在执行时间内数据类型为ftUnkNown,因此出现错误. “ParamType”相同,但默认情况下假定为“ptinput”,所以没有问题. query.ParamByname('ThisValue').DataType := ftString; 您绝对不需要清除参数,因为它已经是NulL.我们怎么知道? IsNull正在返回true …
从TParam.Clear Method:
Use Clear to assign a NulL value to a
parameter.
从TParam.IsNull Property:
Indicates whether the value assigned
to the parameter is NulL (blank).
您绝对不需要绑定参数,因为它完全不相关.当“Bound”为false时,数据集将尝试从该参数的数据源中提供默认值.但是您的数据集甚至没有链接到数据源.从documentation:
[…] Datasets that represent querIEs
and stored procedures use the value of
Bound to determine whether to assign a
default value for the parameter. If
Bound is false,datasets that
represent querIEs attempt to assign a
value from the dataset indicated by
their DataSource property. […]
如果文档不够,请参阅“sqlexpr.pas”中TCustomsqlDataSet.SetParamsFromCursor中的代码.它是dbx框架中唯一引用参数的“Bound”的地方.
总结以上是内存溢出为你收集整理的delphi – 如何显式插入空值到参数化查询?全部内容,希望文章能够帮你解决delphi – 如何显式插入空值到参数化查询?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)