在vc中通过ado的方式联接数据库,正确insert 语句怎么写?(100分)

S

snowbow

Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是interbase数据库,不过请不要建议我还数据库什么的,因为
这不是我定的,并且已经不能改变了。
现在我有数据结构
typedef struct
{
unsigned short type_id;
char type[30];
}Type;
typedef struct
{
unsigned short status_id;
char status[30];
}Status;

typedef struct
{
char pID[19];
TIMESTAMP time;
char place[30];
Type type;
Status status;
}Info;
希望向相应表中insert记录
三个表字段及类型分别为
status:
statusid SMALLINT
status varchar(30)
type:
typeid SMALLINT
type varchar(30)
info :
pid char(18)
place varchar(30)
time TIMESTMAP
typeid SMALLINT
statusid SMALLINT
我的语句
CString strSql;
strSql.Format("insert into LINFO (pid,PLACE,TIME,TYPEID,STATUSID)
VALUES (pid,place,time,info.type.typeid,info.status.status_id)");

bstrSQL = strSql.AllocSysString();
m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
总是不对,为这点事我已经高了一天了,希望众位高手能解救,也不妄我对报的
这个论坛巨大期望。
 
这不是VC论坛,不过有问就答吧。
strSql.Format("insert into LINFO (pid,PLACE,TIME,TYPEID,STATUSID)
VALUES (pid,place,time,info.type.typeid,info.status.status_id)");
这算什麽????你想在strSql.Format()这个函数中写的参数是一个字符串吧?
变量得加在一起才行啊,‘pid,place,time,info.type.typeid,info.status.status_id’
不是字符串吧???这样试试
strSql.Format("insert into LINFO (pid,PLACE,TIME,TYPEID,STATUSID)
VALUES ("+pid+","+place+","+time+","+info.type.typeid
+","+info.status.status_id));
不行,在字符型字段的插入值外加一层引号。
 
不行,其实“”中的部分并不一定都是想你想的字符串,因为它最终被理解为一句sql语句
,然后执行这个语法要求的动作,我现在就是想知道觉得这个语句写的是否正确。因为单纯给
每个变量的值添进去的sql语句,我的是正确的,但是用变量表示就不对了。
 
说的就是这个问题。
变量赋值:
a="abcd";
b="efgh";
c="1";//整型要换成字符串
要使d变量赋值为"select * from table where x1='abcd' and x2='efgh' and x3=1"
d="select * from table where x1='"+a+"' and x2='"+b+"' and x3="+c;
注:意思就是这样,要使d有正确的赋值,表达式要正确,单双引号用的可能有问题。
 
按照你的说法,现在sql语句应该是对的了。但是我执行open语句仍然数据库报错。
错误是个代号,我也不知道什么错误。奇怪的是,我将strSql的内容(在debuger窗口看见的)
考下来,然后直接在sql编辑窗口运行此语句,就能通过。但是一用程序open那个函数就出错。
我就不知道到底哪里出错了。哪位能指点一下呀。我是在vc下用ado方式连的数据库。

 
接受答案了.
 
顶部