将变量传递给SQL [英] Passing a Variable to SQL

查看:116
本文介绍了将变量传递给SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量,它是IP地址.它以文本形式保存在Access 2010数据库中.我正在尝试使用ipSrc运行此查询,并且该查询始终失败.我的猜测是,它将ipSrc视为ipSrc而不是实际的IP地址.我用ipsrc和普通的ipSrc尝试过,但都失败了.还尝试了"ipSrc",也失败了.失败了. "& ipSrc".这是声明.

I have a variable that is an IP address. It is saved as text in my Access 2010 database. I am trying to run this query with ipSrc and the query always fails. My guess is that its seeing ipSrc as ipSrc and not as the actual IP address. I tried it with ''ipSrc'' and just plain ipSrc and both reurn fail. Also tried ""ipSrc"", failed as well. This failed to. ''&ipSrc''. Here is the statement.

SQLCHAR* query = (SQLCHAR*)"SELECT tblIP.[IPAddress], tblIP.[IPType], tblIP.[IPStatus], tblIP.[IPMax] FROM tblIP WHERE tblIP.[IPAddress]= ipSrc AND tblIP.[IPType]=3 AND tblIP.[IPStatus]=1 AND tblIP.[IPMax]=0;";



这是ipSrc的定义.



and here is the definition of ipSrc.

translate_ip(ip_header->source_ip, ipSrc);



使用printf可以将其打印为实际IP地址.



Using printf it prints out as an actual IP address.

printf("\n   Source      IP: %s", ipSrc);

推荐答案

[更新]:
[Updated]:
char buffer[1024] = {0,};
sprintf(buffer,"SELECT tblIP.[IPAddress], tblIP.[IPType], tblIP.[IPStatus], tblIP.[IPMax] FROM tblIP WHERE tblIP.[IPAddress]= %s AND tblIP.[IPType]=3 AND tblIP.[IPStatus]=1 AND tblIP.[IPMax]=0;",ipSrc);
SQLCHAR* query = (SQLCHAR*)buffer;



一行用于sprintf调用

最好的问候
Espen Harlinn



Use a single line for the sprintf call

Best regards
Espen Harlinn


这篇关于将变量传递给SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆