2

I have a simple table called mytable2 with only one column, name as varchar2(20). I now have a list of names stored as vector of std::string to be inserted into the table. I want to use executeArrayUpdate, so I must do the setDataBuffer first. However, as I could see, people always use char[][20] to set databuffer.

This leaves me a big headache, since I have two issues here, first is to convert from vector to array, second is to convert the string to char.

1st, I tired to use vector of char[20], and this doesn't compile. Googled and they say that vector can't take char[], so I changed my vector of std::string to vector of char*.

2nd, I tried to turn the vector to arrray by using "void* p=&names[0]", as some people say this way we can use vectors just as array.

I used stmt->setDataBuffer(1,mystring,OCCI_SQLT_STR,20,NULL), and the program compiled and executed alright, but when I "select name from mytable2", it showed only some strange charaters.

Anyone has had a similiar issue before? what should I do?

My code is simple as below:

    count=2;
    vector<char*> mystring;
    for(int i=0;i<count;i++)
    {
        char my[20];
        strcpy_s(my,"Michael");
        mystring.insert(mystring.end(),my);
             }
    stmt->setDataBuffer(1,&mystring[0],OCCI_SQLT_STR,20,NULL);

    stmt->setBatchErrorMode (true);

    stmt->executeArrayUpdate(count);
Michael
  • 673
  • 2
  • 5
  • 23

1 Answers1

1

You'd need to dynamically create the char array you're putting into the vector for it to have a chance of working correctly.

I have not used OCCI, but if I had to use API that asked for char[][20], I would give it char[][20]

If you have your existing data in vector, why not just copy it across into the 2D char array? Eg.

// intialise vector with nonsense
unsigned int const VEC_SIZE = 2 ;
vector<string> v;
for (unsigned int i = 0; i < VEC_SIZE; ++i) {
    stringstream s;
    s << "Jon Paul " << i << endl;
    v.push_back ( s.str() ) ;
}

// create the required 2D char array
unsigned int const STR_LEN = 20 ;
char c [VEC_SIZE][STR_LEN];

// copy the data from the vector of strings to the 2D char array
for (unsigned int i = 0; i < VEC_SIZE; ++i) {
    string s = v[i].substr(0,STR_LEN);
    char const * const cc = s.c_str();      
    unsigned int j = 0;
    for (; j < STR_LEN && j < s.size(); ++j) {
        c[i][j] = cc[j];
    }
    c[i][ j==STR_LEN ? 20 : j ] = 0; // write NULL character
}

I take it you've simplified your example to be a fixed size vector, so my response is going to be simplified to, with the thorny issue of dynamic allocation of 2D arrays left as an exercise for the reader...

wreckgar23
  • 1,025
  • 9
  • 22
  • Thank you very much for your answer. I was going to wrap the OCCI functions for querying inserting, deleting, updating a table(any table), so that all I have to give is the field names and values, and then the program should automatically read the schema, and generate the sql and insert into oracle. I used to do that in OPN.Net, but I want my program to be high performing so I write everything in C++ again, including algorithms, databases, webservice, etc. I think that I may need to write some function to convert these vector to something like char**? – Michael Jun 03 '12 at 09:14
  • Yes. You could probably make life easier for yourself by making your conversion function also make the call to OCCI. Before embarking on all that, I'd want to know that reimplementing everything in C++ would make things any quicker. You need some way of analysing what the bottlenecks are in your existing .Net code. – wreckgar23 Jun 03 '12 at 11:40
  • what if I don't know max size of strings? – Arkady Jan 03 '18 at 16:38
  • @Arkady the max size of what string? If you're asking about OCCI I'm not familiar with it and you're best off asking a new question – wreckgar23 Jan 07 '18 at 21:05