I requested for materials that will help me migrate from the use of C/C++ i/o to use of databases like sql server, oracle etc. I got some good materials. But I have
problems understanding some of the codes.Shown below some samples of the codes. Everything is clear and understandable to me except stated code line.What is fblob.What is test_tbl.
Can any one explain the stated code lines to me.
#include <stdio.h> // for printf
#include <sqlapi.h> // main SQLAPI++ header
int main(int argc, char* argv[])
{
SAConnection con; SACommand cmd;
try
{
con.Connect("test", "tester", "tester", SA_Oracle_Client);
cmd.setConnection(&con);
cmd.setCommandText(
"Create table test_tbl(fid integer, fvarchar20 varchar(20), fblob blob)");
cmd.Execute();
cmd.setCommandText(
"Insert into test_tbl(fid, fvarchar20) values (1, 'Some string (1)')");
cmd.Execute();
con.Commit();
printf("Table created, row inserted!\n");
}
catch(SAException &x)
{
try
{
con.Rollback();
}
catch(SAException &)
{
}
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
#include <stdio.h> // for printf
#include <sqlapi.h> // main SQLAPI++ header
int main(int argc, char* argv[])
{
SAConnection con; SACommand cmd;
try
{
con.Connect("test", "tester", "tester", SA_Oracle_Client);
cmd.setConnection(&con);
cmd.setCommandText(
"Insert into test_tbl(fid, fvarchar20) values(:1, :2)");
cmd.Param(1).setAsLong() = 2;
cmd.Param(2).setAsString() = "Some string (2)";
cmd.Execute();
cmd << (long)3 << "Some string (3)";
cmd.Execute();
con.Commit();
printf("Input parameters bound, rows inserted!\n");
}
catch(SAException &x)
{
try
{
con.Rollback();
}
catch(SAException &)
{
}
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
#include <stdio.h> // for printf
#include <sqlapi.h> // main SQLAPI++ header
int main(int argc, char* argv[])
{
SAConnection con; SACommand cmd(
&con,
"Select fid, fvarchar20 from test_tbl");
try
{
con.Connect("test", "tester", "tester", SA_Oracle_Client);
cmd.Execute();
while(cmd.FetchNext())
{
printf("Row fetched: fid = %ld, fvarchar20 = '%s'\n",
cmd.Field("fid").asLong(),
(const char*)cmd.Field("fvarchar20").asString());
}
con.Commit();
printf("Rows selected!\n");
}
catch(SAException &x)
{
try
{
con.Rollback();
}
catch(SAException &)
{
}
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
#include <stdio.h> // for printf
#include <sqlapi.h> // main SQLAPI++ header
void IntoFileReader(
SAPieceType_t ePieceType,
void *pBuf,
unsigned int nLen,
unsigned int nBlobSize,
void *pAddlData);
int main(int argc, char* argv[])
{
SAConnection con;
SACommand cmd(
&con,
"Select fblob from test_tbl");
try
{
con.Connect("test", "tester", "tester", SA_Oracle_Client);
cmd.Execute();
while(cmd.FetchNext())
{
SAString s = cmd.Field("fblob").asBLob();
printf("Size of BLob is %d bytes\n", s.GetLength());
}
cmd.Execute();
cmd.Field("fblob").setLongOrLobReaderMode(SA_LongOrLobReaderManual);
SAString sFilename;
int i = 0;
while(cmd.FetchNext())
{
sFilename.Format("fblob%d.bin", ++i);
if(!cmd.Field("fblob").isNull())
cmd.Field("fblob").ReadLongOrLob(
IntoFileReader, 10*1024, (void*)(const char*)sFilename );
}
con.Commit();
printf("Rows with BLob field fetched!\n");
}
catch(SAException &x)
{
try
{
con.Rollback();
}
catch(SAException &)
{
}
printf("%s\n", (const char*)x.ErrText());
}
return 0;
}
static FILE *pFile = NULL;
static int nTotalRead;
void IntoFileReader(
SAPieceType_t ePieceType,
void *pBuf,
unsigned int nLen,
unsigned int nBlobSize,
void *pAddlData)
{
if(ePieceType == SA_FirstPiece || ePieceType == SA_OnePiece)
{
nTotalRead = 0;
const char *sFilename = (const char *)pAddlData;
pFile = fopen(sFilename, "wb");
if(!pFile)
SAException::throwUserException(-1, "Can not open file '%s' for writing", sFilename);
}
fwrite(pBuf, 1, nLen, pFile);
nTotalRead += nLen;
printf("%d bytes of %d read\n", nTotalRead, nBlobSize);
if(ePieceType == SA_LastPiece || ePieceType == SA_OnePiece)
{
fclose(pFile);
pFile = NULL;
}
}