Access to binary databases

Source: Internet
Author: User
Tags import database

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.