Database Access in C

TheOriginalOne

Active Member
Hi, Listers!

We are not a team of C developers but we need to make a mod to C program. We are having a heck of a time opening a file and reading a value from a file. We've read the Help in OneWorld, reviewed the API guide, looked online, yet this thing is beating us with a club. Could you read the following snippet and offer any clues as to what's wrong? I'll be more than happy to provide any supplemental information that may be required.

Thanks a lot, everyone. We do appreciate it!

<font class="small">Code:</font><hr /><pre>idReturnCode = JDB_OpenTable (hUser, NID_F5503WPT, 0, NULL, (ushort) 0, (char *) NULL, &hRequestF5503WPT);

if (idReturnCode == ER_SUCCESS)
{
memcpy((void *)(&dsKeyF5503WPT.pttrar), (const void *)(dsD0000163.szPaymentTermsCode01), sizeof(dsD0000163.szPaymentTermsCode01));
idJDEDBReturn = JDB_FetchKeyed(hRequestF5503WPT, (ID) 0, &dsKeyF5503WPT, (short) 1, &dsF5503WPT, FALSE);
JDB_CloseTable (hRequestF5503WPT);
} </pre><hr />
 
At first glance looks fine.

What part of it is failing?

You may want to change
<font class="small">Code:</font><hr /><pre>
memcpy((void *)(&dsKeyF5503WPT.pttrar), (const void *)(dsD0000163.szPaymentTermsCode01), sizeof(dsD0000163.szPaymentTermsCode01));
</pre><hr />
to:

<font class="small">Code:</font><hr /><pre>
memcpy((void *)(dsKeyF5503WPT.pttrar), (const void *)(dsD0000163.szPaymentTermsCode01), sizeof(dsKeyF5503WPT.pttrar));

//actually you can do this as well...

memcpy(dsKeyF5503WPT.pttrar, dsD0000163.szPaymentTermsCode01, sizeof(dsKeyF5503WPT.pttrar));

//strictly speaking the cast to voids is not really necessary and I find that code is easier to read without a bunch of unnessary //casts
</pre><hr />


Notice the missing "&" on dsKeyF5503WPT.pttrar. This is assuming that pttrar is a string just like szPaymentTermsCode01. If pttrar is NOT a string, well there lies you problem. However, removing the "&" may not make a big difference.... the compiler may actually kind of ignore your "&" resolve the memory address to the array address. Also notice that the sizeof is on the arg you are copying to. If both params are the same DD then it doesnt matter, its just good practice.

or better yet:

<font class="small">Code:</font><hr /><pre>
jdeStrncpy(dsKeyF5503WPT.pttrar, dsD0000163.szPaymentTermsCode01, DIM(dsKeyF5503WPT.pttrar)-1); /* for unicode releases */
strncpy(dsKeyF5503WPT.pttrar, dsD0000163.szPaymentTermsCode01, sizeof(dsKeyF5503WPT.pttrar)-1); /* for non-unicode releases like Xe
*/
</pre><hr />
 
oh... just saw your problem.

if (idReturnCode == ER_SUCCESS)

s/b

if (idReturnCode == JDEDB_PASSED)
 
BOster,

Thanks for the attempt! We realized that the "Behavior" section needed to be initialized first. Without initialization, certain parms were invalid. We thought the "Behavior" was already set, but as I said, we are not C developers by any stretch of the imagination.

Bottom line: We got this one, but what a fight we had!
 
Back
Top