使用时OCILIB填充字符串缓冲区 [英] Filling a string buffer when using Ocilib
问题描述
我使用 OCILIB 到Oracle数据库上执行批量插入,但我有一些麻烦却使一个字符串缓冲区。
I'm using Ocilib to perform a bulk insert on a Oracle database but I'm having some trouble while filling a string buffer.
该文件说:
有关串/ RAW阵列,输入数组
必须是一个连续的数据块,并
不是一个指针数组。因此,要结合
10个元件为一个阵列
VARCHAR2(30)列,绑定变量
必须是像阵列[10] [31]
For string/RAW arrays, the input array MUST BE a contiguous block of data and not an array of pointers. So to bind an array of 10 elements for a varchar2(30) column, binded variable must be a like array[10][31]
和样本收益来填补这样一个缓冲区:
And a sample proceeds to fill a buffer like this:
...
char tab_str[1000][21];
...
OCI_BindArrayOfStrings(st, ":s", (char*) tab_str, 20, 0);
...
for(i=0;i<1000;i++)
{
sprintf(tab_str[i],"Name %d",i+1);
}
...
我试图填补了字符串缓冲区,同时通过的MyClass一个std ::矢量循环。 MyClass的有一个std :: string成员。
I'm trying to fill the string buffer while looping through a std::vector of MyClass. MyClass has a std::string member.
我尝试使用std :: string的一份::法在字符串内容到缓冲区拷贝。但我不知道究竟如何索引到缓冲区做到这一点。
I'm trying to use the std::string::copy method to copy over the string contents to the buffer. But I don't know exactly how to index the buffer to do it.
...
OCI_BindArrayOfStrings(st, ":f2", NULL, VCHAR_SIZE, 0);
char** vc_buffer = (char**)OCI_BindGetData(OCI_GetBind(st, 2));
...
int i = 0;
for(vector<MyClass>::const_iterator it = myVec.begin(); it != myVec.end(); ++it)
{
/* 1st try */ it->m_string.copy((vc_buffer + (i * VCHAR_SIZE)), VCHAR_SIZE);
/* 2nd try */ it->m_string.copy(vc_buffer[i], VCHAR_SIZE);
++i;
...
}
...
第一种方式让我在数据库中的数据马车。第二个让我打了一个空指针。
The first way gives me buggy data in the database. The second makes me hit a null pointer.
我在做什么错了?
PS
第二种方法,沿着下面亚历山德罗Vergani提出的方法,结果空字符串插入。第一种方法给出了这样的(有点离奇的)结果是:
The second approach, along the approach proposed by Alessandro Vergani below, results in null strings inserted. The first approach gives this (somewhat bizarre) result:
在gvim的窗口显示了它应该是什么样子,顶点屏幕显示什么在数据库中结束。
The gvim window shows what it's supposed to look like, the apex screen shows what ends up in the database.
推荐答案
(尝试:
std::vector<char> tab_str(myVec.size() * (VCHAR_SIZE + 1));
...
OCI_BindArrayOfStrings(st, ":s", &tab_str[0], VCHAR_SIZE, 0);
...
int offset = 0;
for(vector<MyClass>::const_iterator it = myVec.begin(); it != myVec.end(); ++it, offset += VCHAR_SIZE)
{
it->m_string.copy(&tab_str[offset], VCHAR_SIZE);
...
}
...
我不知道你需要添加空终止:如果没有,删除 1
从复制和删除第二行
这篇关于使用时OCILIB填充字符串缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!