语句handle(hstmt)到底是做什么的?在ODBC中 [英] exactly what does the statement handle(hstmt) do? in ODBC

查看:196
本文介绍了语句handle(hstmt)到底是做什么的?在ODBC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始为ODBC编写C代码 但是我有一个问题 在搜索了C的odbc函数教程之后,我仍然无法准确获得 他们如何协同工作...

I`m new to write c code for ODBC but here I have question... after searching for the tutorial for odbc function tutorials for C, I still caanot get exactly how they work collaborately...

该语句处理hstmt对数据库数据到底有什么作用? 我知道它可以控制我作为参数给出的SQL查询语句.但,, 那么我作为查询给出的每个SQL语句都应该为每个语句都具有handle(hstmt)?还是几个SQL查询语句的一个语句句柄?

what does the statement handle hstmt exactly do for database data? I knew it controls the SQL query statements that I give as a parameter. but,, then every SQL statements that i give as query should have each statement handle(hstmt) for each of them? or one statement handle for several SQL query statements?

例如,

lstrcpy((LPTSTR)update, L"insert into employee values  ('Dshong','summer','LosAngeles');");
SQLExecDirect(hstmt1, update, SQL_NTS);

lstrcpy((LPTSTR)update, L"insert into works values ('Dshong','Small Bank',    2500);");
SQLExecDirect(hstmt3, update1, SQL_NTS);

lstrcpy((LPTSTR)select, L"select * from works;");
if (SQLExecDirect(hstmt, select, SQL_NTS) != SQL_SUCCESS)
    return printf("can’t exec direct");


lstrcpy((LPTSTR)select1, L"select * from employee;");
if (SQLExecDirect(hstmt2, select1, SQL_NTS) != SQL_SUCCESS)
    return printf("can’t exec direct");

我应该这样给两个带有四个hstmt的insert语句和两个select语句吗?

should I give like this for two insert statements and two select statements with four hstmt?

如果我给这样的话,

lstrcpy((LPTSTR)insert, L"insert into employee values ('Dshong','summer','LosAngeles');");
SQLExecDirect(hstmt1, insert, SQL_NTS);

SQLExecDirect具有hstmt1作为参数,但我不知道SQLExecDirect如何使用它..

the SQLExecDirect has hstmt1 as parameter but I don`t know how SQLExecDirect works with it..

而且我认为结果应该插入一行,不是吗?

And as I think, the result should be one line inserted, isnt it?

但是当我运行代码时,它为('Dshong','summer','LosAngeles')插入了多个相同的行... 当我打印出结果时,多次插入了('Dshong','summer','LosAngeles') 即使插入行不在for循环中……

but when I run the code, it inserts multi-same rows for ('Dshong','summer','LosAngeles')... when I printed out the result, ('Dshong','summer','LosAngeles') was inserted multiple times even though the insert line was not in the for loop or so...

以及它们如何与以下功能一起使用?

and how they work together with the functions below?

  • SQLBindCol(hstmt, 1, SQL_C_CHAR, id, (SDWORD)sizeof(id), &idlen);
  • SQLFetch(hstmt)
  • SQLBindCol(hstmt, 1, SQL_C_CHAR, id, (SDWORD)sizeof(id), &idlen);
  • SQLFetch(hstmt)

我知道他们只是在做什么,但是..在这里,我想确切地知道用哪种方式 hstmt(语句句柄)与它们一起工作..

I know what they simply doing but.. here I want to know exactly in which way hstmt ( statement handle) work with them..

谢谢你. :)

推荐答案

C中的句柄只是指向由ODBC分配的内存块的指针.通常,一个可跟踪SQL查询状态的结构. INSERT语句中的句柄用处不大,只需使用一次即可.除非失败,否则您需要将 exact 相同的句柄传递给SQLError(),以找出问题所在.然后使用哪个ODBC访问该内部结构并检索错误代码.与结构指针相比,句柄的优势在于它隐藏了内部实现.

A handle in C is just a pointer to a chunk of memory that's allocated by ODBC. Typically a struct that keeps track of the state of a SQL query. You don't have much use for the handle in an INSERT statement, you use it just once. Unless it fails, then you need to pass the exact same handle to SQLError() to find out what went wrong. Which ODBC then uses to access that internal struct and retrieve the error code. The advantage of a handle over a struct pointer is that it hides the internal implementation.

您肯定需要重复将其用于SELECT查询,因为您要检索查询结果.您必须将 exact 相同的句柄传递给SQLBindCol()才能将查询结果中的列映射到内存位置.并使用SQLFetch()来检索行.

You certainly need to use it repeatedly for a SELECT query since you want to retrieve the query result. You must pass the exact same handle to SQLBindCol() to map a column in the query result to a memory location. And to SQLFetch() to retrieve a row.

如果您了解C ++,那么将句柄视为 this 对象指针可能会有所帮助.和SQLAllocStmt()作为构造函数.而SQLFetch()作为该类的实例方法.最后是SQLFreeHandle()作为析构函数.就像MFC的CRecordSet,CDatabase来包装SQLHDBC句柄一样,这正是C ++ ODBC包装类的作用.

If you know C++ then it can be helpful to think of a handle as the this object pointer. And SQLAllocStmt() as the constructor. And SQLFetch() as an instance method of the class. And finally SQLFreeHandle() as the destructor. Which is exactly what a C++ ODBC wrapper class does, like MFC's CRecordSet, CDatabase to wrap a SQLHDBC handle.

在大多数情况下,您仅使用单个SQLHSTMT句柄.除非您同时运行多个SQL语句.说要从SELECT查询生成UPDATE语句,该查询需要两个句柄.

In most cases you only use a single SQLHSTMT handle. Unless you run multiple SQL statements at the same time. Say to generate UPDATE statements from a SELECT query, that requires two handles.

这篇关于语句handle(hstmt)到底是做什么的?在ODBC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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