0

The function prototype is as follows:

SQLRETURN SQLBindParameter(
      SQLHSTMT        StatementHandle,
      SQLUSMALLINT    ParameterNumber,
      SQLSMALLINT     InputOutputType,
      SQLSMALLINT     ValueType,
      SQLSMALLINT     ParameterType,
      SQLULEN         ColumnSize,
      SQLSMALLINT     DecimalDigits,
      SQLPOINTER      ParameterValuePtr,
      SQLLEN          BufferLength,
      SQLLEN *        StrLen_or_IndPtr);

I am more interested in parameters 6,8 9 and 10 and how they apply to variable strings. Other built-in data types seem less problematic All the examples I have seen on the internet e.g. http://msdn.microsoft.com/en-us/library/ms709287(v=vs.85).aspx use static arrays which seems straight forward. But I want to use a std::vector and all my attempts have failed.

So say I want to bind these strings.

std::vector<std::string> countries = boost::assign::list_of("Argentina")( "Burkina Faso")( "China")( "Dominica Republic");

Note: I am using std::vector<string> for conveniece of declaration. I cannot send c.str() to the database the driver should be able to modify the string in some situations

So I declare a buffer like this

std::vector<char> my_data;

and copy my strings to the buffer.
How then should I pass these parameters:

ColumnSize ?
ParameterValuePtr ? // this I think I am sure should be &my_data[0]
BufferLength ?
ashley
  • 1
  • 1
  • `vector` is modifiable. Why are you using `vector` isn't quite clear to me. And passing of a the address location of string should work as you expected ( string is nothing but an array of characters ) – Mahesh Sep 15 '11 at 15:30
  • @Mahesh Are you telling me you can pass &my_data[0] where my_data is declared as std::vector to a c function that may modify the data? Would this memory be contiguous? Please, provide an example in an answer if you can – ashley Sep 15 '11 at 15:38
  • @Mahesh: It's not _quite_ that simple. – Lightness Races in Orbit Sep 15 '11 at 17:21

1 Answers1

0

Note: I am using std::vector for conveniece of declaration. I cannot send c.str() to the database the driver should be able to modify the string in some situations

If you're using C++11, then you may do std::string& str = myVectorOfStrings[i]; char* buf = &str[0]; no copy required.

If you're using C++03 it's a little more debatable as to how safe this is. Strictly speaking strings weren't guaranteed to be contiguous back then, but many of us decided that — for various reasons — it would be safe enough to assume it anyway.

If you're not happy with that, then your current approach is pretty much correct... and then, yes, char* buf = &myVectorOfCopiedChars[0] is right.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Further information on `std::string`: http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/#comment-483 – Lightness Races in Orbit Sep 15 '11 at 17:28
  • I am using C++03. But even if the memory of std::string is contiguous, is it modifiable by a c- function? Also would you have any suggestion on how I should pass the ColumnsSize and the BufferLength? – ashley Sep 15 '11 at 17:36
  • @ashley: Yes; C++ standard library container elements are mutable, with the exception of `std::set`/`std::multiset` (and the unordered varieties) and the _key_ parts of `std::map` (and varieties). – Lightness Races in Orbit Sep 15 '11 at 17:44