勇芳软件工作室.汉化:  SQLite3 API Functions > Blobs >

sqlite3_blob_write

Previous pageReturn to chapter overviewNext page

描述

 

该函数用于将数据写入来自调用者提供的缓冲区的BLOB句柄。的numBytessv个字节的数据从缓冲区szBuffer复制到打开的BLOB中,从偏移量n偏移开始。

 

C / C ++语法

 

int sqlite3_blob_write(

sqlite3_blob *,

const void *z,

int n,

int iOffset

);

 

PB语法

 

FUNCTION sqlite3_blob_write ( _

BYVAL pBlob AS DWORD,

BYREF szBuffer AS ANY, _

BYVAL numBytes AS LONG, _

BYVAL nOffset AS LONG _

) AS LONG

 

参数

 

pBlob

 

[in]blob句柄。

 

szBuffer

 

[in]缓冲区。

 

numBytes

 

[in]缓冲区的大小(以字节为单位)。

 

nOffset

 

[in]起始位置。如果偏移量n偏移从BLOB结尾小于的numBytessv个字节,则返回SQLITE_ERROR,不会读取数据。如果的numBytessvn偏移小于零,则返回SQLITE_ERROR,而不会读取任何数据。可以使用sqlite3_blob_bytes函数确定blob的大小(从而numBytes + nOffset的最大值)。

 

返回值

 

SQLITE_OK成功;否则返回错误代码或扩展错误代码。

 

如果作为第一个参数传递的BLOB句柄未被打开以进行写入(flags参数为sqlite3_blob_open为零),则此函数返回SQLITE_READONLY。

 

备注

 

此函数只能修改BLOB的内容;使用该API不可能增加BLOB的大小。如果偏移量iOffset从BLOB的结尾小于N个字节,则返回SQLITE_ERROR,并且不会写入任何数据。如果N小于0,则返回SQLITE_ERROR,并且不会写入任何数据。可以使用sqlite3_blob_bytes函数确定BLOB的大小(因此numBytes + nOffset的最大值)。

 

C ++实现代码

 

/*

** Perform a read or write operation on a blob

*/

static int blobReadWrite(

sqlite3_blob *pBlob,

void *z,

int n,

int iOffset,

int (*xCall)(BtCursor*, u32, u32, void*)

){

int rc;

Incrblob *p = (Incrblob *)pBlob;

Vdbe *v;

sqlite3 *db;

 

if( p==0 ) return SQLITE_MISUSE_BKPT;

db = p->db;

sqlite3_mutex_enter(db->mutex);

v = (Vdbe*)p->pStmt;

 

if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){

  /* Request is out of range. Return a transient error. */

  rc = SQLITE_ERROR;

  sqlite3Error(db, SQLITE_ERROR, 0);

}else if( v==0 ){

  /* If there is no statement handle, then the blob-handle has

  ** already been invalidated. Return SQLITE_ABORT in this case.

  */

  rc = SQLITE_ABORT;

}else{

  /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is

  ** returned, clean-up the statement handle.

  */

  assert( db == v->db );

  sqlite3BtreeEnterCursor(p->pCsr);

  rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);

  sqlite3BtreeLeaveCursor(p->pCsr);

  if( rc==SQLITE_ABORT ){

    sqlite3VdbeFinalize(v);

    p->pStmt = 0;

  }else{

    db->errCode = rc;

    v->rc = rc;

  }

}

rc = sqlite3ApiExit(db, rc);

sqlite3_mutex_leave(db->mutex);

return rc;

}

 

/*

** Write data to a blob handle.

*/

SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){

return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);

}