位元據庫的存取

來源:互聯網
上載者:User

標籤:

一:將資料以二進位的形式儲存到資料庫

 這裡,我們以float一維數組的形式儲存多個點的座標: 

思路:先將float數組轉化為char數組,然後將其轉化為safeArray並儲存到BLOB資料中,然後儲存在資料庫中的image欄位中。 

1:將float數組轉化為char數組:

函數

輸入:flaot一維數組  數組中包含個數n

輸出:char一維數組

具體代碼:

char *ptVal = new char[n*sizeof(float)];//用於儲存轉化得到的char數組

       for(int i = 0; i<n; i++)

       {  

              char *m = new char[sizeof(float)];

              float temp = f[i];//每次從float數組中取出一個對象

              memcpy(m,&temp,sizeof(float));//將float轉化為一個4個位元組的char數組 

              int j = 0;

        //將轉化得到的char數組插入ptVal 數組的對應位置

              while (j<sizeof(float))

              {

                     ptVal[4*i+j] = m[j];

                     j++;

              }     

              delete m;

       }

 2:將char數組轉化為safeArray數群組類型,然後轉化為BLOB數群組類型並匯入資料庫

輸入:char數組  數組對應位元組數blobSize   資料庫中對應欄位的位置iCol

功能:將char數組轉化為二進位並插入到資料庫中

具體代碼:

long i, rtn;

       char *pMove = NULL;

       _variant_t  vVal;

       SAFEARRAYBOUND  rgsabound[1];

       SAFEARRAY    *psa = NULL; 

       pMove = ptVal;

       //1. 建立SAFEARRAY對象

       rgsabound[0].lLbound = 0; //定義數組的下界

       rgsabound[0].cElements = blobSize; //定義數組元素的大小

       psa = SafeArrayCreate(VT_UI1,1,rgsabound); 

       for (i = 0; i < blobSize; i++)

       {

              if (FAILED(SafeArrayPutElement (psa, &i, pMove++)))

                     return (0);

       }

    //將資料存放區為BLOB類型

       vVal.vt = VT_ARRAY | VT_UI1;

       vVal.parray = psa;

   //將位元據加入到資料庫

       try

       {

              Fields   *pFields   =   m_pRecordset-> GetFields();

              Field   *pField   =   pFields-> GetItem( iCol);

              pField->AppendChunk(vVal);

       // m_pRecordset->GetFields()->GetItem(iCol)->AppendChunk(vVal); 

       }

     //捕捉錯誤

       catch (_com_error &e)

       {

              AfxMessageBox(e.Description());

              return(0);

       }

       return (1);

二:從資料庫中取出資料

思路:從資料庫中取出資料到一個char數組的緩衝區中,然後解碼還原成float數組 

1:從資料庫中取出資料到緩衝區中

 實現過程:   

     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); //複製資料到緩衝區pBuf

                     SafeArrayUnaccessData(val.parray); 

從資料庫中取出資料到float數組中:

實現過程:

       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;

              }

位元據庫的存取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.