⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 step5.cpp

📁 通用的数据库中间库
💻 CPP
字号:
#include <stdio.h>  // for printf
#include <SQLAPI.h> // main SQLAPI++ header

// forwards
SAString ReadWholeFile(const char *sFilename);
unsigned int FromFileWriter(
	SAPieceType_t &ePieceType,
	void *pBuf, unsigned int nLen, void *pAddlData);

int main(int argc, char* argv[])
{
    SAConnection con;		// connection object
	// command object
	// update fblob field of our test table
    SACommand cmd(&con, 
		"Update test_tbl set fblob = :fblob where fid =:1");
    
    try
    {
        // connect to database (Oracle in our example)
        con.Connect("test", "tester", "tester", SA_Oracle_Client);
		
        // use first method of Long(Lob) binding - as a whole
        cmd.Param(1).setAsLong() = 1;	// fid
        cmd.Param("fblob").setAsBLob() = ReadWholeFile("test.doc");
        // update first row
        cmd.Execute();
		
        // use second method of binding - user defined callbacks
        cmd.Param(1).setAsLong() = 2;
		cmd.Param("fblob").setAsBLob(
			FromFileWriter,	// our callback to provide blob data from file
			10*1024,		// desired size of each piece
			(void*)"test.doc");	// additional param, file name in our case
        // update second row
        cmd.Execute();	// at that moment Library will call user callback when needed
		
        // commit changes on success
        con.Commit();
		
        printf("Blob parameter binded, rows updated!\n");
    }
    catch(SAException &x)
    {
        // SAConnection::Rollback()
        // can also throw an exception
        // (if a network error for example),
        // we will be ready
        try
        {
            // on error rollback changes
            con.Rollback();
        }
        catch(SAException &)
        {
        }
        // print error message
        printf("%s\n", (const char*)x.ErrText());
    }
    
    return 0;
}

SAString ReadWholeFile(const char *sFilename)
{
	SAString s;
	char sBuf[32*1024];
	FILE *pFile = fopen(sFilename, "rb");

	if(!pFile)
		SAException::throwUserException(-1, 
		"Error opening file '%s'\n", sFilename);
	do
	{
		unsigned int nRead = fread(sBuf, 1, sizeof(sBuf), pFile);
		s += SAString(sBuf, nRead);
	}
	while(!feof(pFile));
	
	fclose(pFile);
	
	return s;
}

static FILE *pFile = NULL;
static int nTotalBound;
unsigned int FromFileWriter(
	SAPieceType_t &ePieceType,
	void *pBuf, unsigned int nLen, void *pAddlData)
{
	if(ePieceType == SA_FirstPiece)
	{
		const char *sFilename = (const char *)pAddlData;
		pFile = fopen(sFilename, "rb");
		if(!pFile)
			SAException::throwUserException(-1, "Can not open file '%s'", sFilename);
		
		nTotalBound = 0;
	}
	
	unsigned int nRead = fread(pBuf, 1, nLen, pFile);
	nTotalBound += nRead;
	// show progress
	printf("%d bytes of file bound\n", nTotalBound);
	
	if(feof(pFile))
	{
		ePieceType = SA_LastPiece;
		fclose(pFile);
		pFile = NULL;
	}
	
	return nRead;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -