One: Store data in binary form in the database
Here, we store the coordinates of multiple points in the form of a one-dimensional array of float:
Idea: First convert the float array into a char array, then convert it to SAFEARRAY and store it in BLOB data, and then store it in the Image field in the database.
1: Convert the float array to a char array:
Function
Input: Flaot contains the number of n in a one-dimensional array array
Output: Char one-dimensional array
Specific code:
Char *ptval = new char[n*sizeof (float)];//used to store the converted Char array
for (int i = 0; i<n; i++)
{
Char *m = new char[sizeof (float)];
Float temp = f[i];//Removes an object from the float array every time
memcpy (m,&temp,sizeof (float));//convert float to a 4-byte char array
int j = 0;
Inserts the converted char array into the corresponding position of the Ptval array
while (j<sizeof (float))
{
PTVAL[4*I+J] = M[j];
j + +;
}
Delete m;
}
2: Convert char array to SAFEARRAY array type, then convert to BLOB array type and import Database
Input: Char array corresponds to the number of bytes blobsize the location of the corresponding field in the database Icol
Function: Converts a char array to binary and inserts into the database
Specific code:
Long I, RTN;
char *pmove = NULL;
_variant_t Vval;
Safearraybound rgsabound[1];
SAFEARRAY *PSA = NULL;
Pmove = Ptval;
1. Create a SafeArray object
Rgsabound[0].llbound = 0; Defines the lower bound of an array
Rgsabound[0].celements = blobsize; To define the size of an array element
PSA = SafeArrayCreate (vt_ui1,1,rgsabound);
for (i = 0; i < blobsize; i++)
{
if (FAILED (SafeArrayPutElement (PSA, &i, pmove++))
return (0);
}
Storing data as BLOB type
VVAL.VT = Vt_array | VT_UI1;
Vval.parray = PSA;
Adding binary data to a database
Try
{
Fields *pfields = M_precordset-> getfields ();
Field *pfield = pfields-> GetItem (icol);
Pfield->appendchunk (Vval);
M_precordset->getfields ()->getitem (icol)->appendchunk (vval);
}
Catch error
catch (_com_error &e)
{
AfxMessageBox (E.description ());
return (0);
}
return (1);
Second: Extracting data from the database
Idea: Take the data from the database into a buffer of a char array, then decode and restore to a float array
1: Extracting data from the database into the buffer
Implementation process:
Fields *pflds;
Pflds = M_precordset->getfields ();
Long ldatasize = Pflds->getitem ("Data")->actualsize;
_variant_t num = pflds->getitem ("num")->getvalue ();
int n = num.intval;
Char *pbuf;
if (fp!=null) Delete FP;
fp = new Float[n];
float test;
if (ldatasize>0)
{
_variant_t val = Pflds->getitem ("Data")->getchunk (ldatasize);
if (VAL.VT = = (vt_array| VT_UI1))
{
PBuf = new Char[ldatasize+1];
char *pmove = NULL;
Safearrayaccessdata (Val.parray, (void * *) &pmove);
memcpy (pbuf,pmove,ldatasize); Copy data to buffer Pbuf
Safearrayunaccessdata (Val.parray);
Remove data from the database into the float array:
Implementation process:
for (int i = 0; i<n; i++)
{
Char *m = new CHAR[4];
int j = 0;
while (J<4)
{
M[J] = Pbuf[4*i+j];
j + +;
}
memcpy (&test,m,4);
Fp[i] = test;
Delete m;
}
delete [] pBuf;
}
Access to binary databases