Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.




C++
#include <stdio.h>  // for printf
#include <sqlapi.h> // main SQLAPI++ header

int main(int argc, char* argv[])
{
    SAConnection con; // connection object
    SACommand cmd;    // create command object
    
    try
    {
        // connect to database (Oracle in our example)
        con.Connect("test", "tester", "tester", SA_Oracle_Client);
        // associate a command with connection
        // connection can also be specified in SACommand constructor
        cmd.setConnection(&con);

        //I don't uderstand this line// create table
        cmd.setCommandText(
            "Create table test_tbl(fid integer, fvarchar20 varchar(20), fblob blob)");
        cmd.Execute();

        // insert value
        cmd.setCommandText(
            "Insert into test_tbl(fid, fvarchar20) values (1, 'Some string (1)')");
        cmd.Execute();

        // commit changes on success
        con.Commit();

        printf("Table created, row inserted!\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;
}






#include <stdio.h>  // for printf
#include <sqlapi.h> // main SQLAPI++ header

int main(int argc, char* argv[])
{
    SAConnection con; // connection object
    SACommand cmd;    // command object
    
    try
    {
        // connect to database (Oracle in our example)
        con.Connect("test", "tester", "tester", SA_Oracle_Client);
        // associate a command with connection
        cmd.setConnection(&con);

        //(I don't understand this line)// Insert 2 rows
        cmd.setCommandText(
            "Insert into test_tbl(fid, fvarchar20) values(:1, :2)");

        // use first method of binding - param assignment
        cmd.Param(1).setAsLong() = 2;
        cmd.Param(2).setAsString() = "Some string (2)";
        // Insert first row
        cmd.Execute();

        // use second method of binding - stream binding
        cmd << (long)3 << "Some string (3)";
        // Insert second row
        cmd.Execute();

        // commit changes on success
        con.Commit();

        printf("Input parameters bound, rows inserted!\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;
}

#include <stdio.h>  // for printf
#include <sqlapi.h> // main SQLAPI++ header

int main(int argc, char* argv[])
{
    SAConnection con; // connection object
    SACommand cmd(
        &con,
        "Select fid, fvarchar20 from test_tbl");    // command object
    
    try
    {
        // connect to database (Oracle in our example)
        con.Connect("test", "tester", "tester", SA_Oracle_Client);

        // Select from our test table
        cmd.Execute();
        // fetch results row by row and print results
        while(cmd.FetchNext())
        {
            printf("Row fetched: fid = %ld, fvarchar20 = '%s'\n", 
                cmd.Field("fid").asLong(),
                (const char*)cmd.Field("fvarchar20").asString());
        }

        // commit changes on success
        con.Commit();

        printf("Rows selected!\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;
}


#include <stdio.h>  // for printf
#include <sqlapi.h> // main SQLAPI++ header

// forwards
void IntoFileReader(
	SAPieceType_t ePieceType,
	void *pBuf,
	unsigned int nLen,
	unsigned int nBlobSize,
	void *pAddlData);

int main(int argc, char* argv[])
{
    SAConnection con; // connection object
   
//I don't understand this line
 SACommand cmd(
        &con,
        "Select fblob from test_tbl");    // command object
    
    try
    {
        // connect to database (Oracle in our example)
        con.Connect("test", "tester", "tester", SA_Oracle_Client);

        // Usage 1. Read whole BLob(s) into internal buffers
		// Select BLob from our test table
        cmd.Execute();
        // fetch results row by row and print results
        while(cmd.FetchNext())
        {
			// after fetching a row all Long/Lob fields are automatically read into //internal buffers
			// just like other data types
			SAString s = cmd.Field("fblob").asBLob();
            printf("Size of BLob is %d bytes\n", s.GetLength());
        }

        // Usage 2. Read BLob in pieces providing user callback for BLob data processing
		// Select blob from our test table
        cmd.Execute();
		// do not automatically read this field into internal buffer (into corresponding SAField object)
		// we will provide a callback for BLob fetching after FetchNext
		cmd.Field("fblob").setLongOrLobReaderMode(SA_LongOrLobReaderManual);
        
		// fetch results row by row and print results
		SAString sFilename;
		int i = 0;
        while(cmd.FetchNext())
        {
			sFilename.Format("fblob%d.bin", ++i);
			// at that moment all fields are fetched except
			// those that set for manual retrieving
			// read them (fblob in our example) now
			if(!cmd.Field("fblob").isNull())
				cmd.Field("fblob").ReadLongOrLob(
					IntoFileReader,	// our callback to read BLob content into file
					10*1024,		// our desired piece size
					(void*)(const char*)sFilename	// additional data, filename in our example
					);
		}

        // commit changes on success
        con.Commit();

        printf("Rows with BLob field fetched!\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;
}

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;

	// show progress
	printf("%d bytes of %d read\n", nTotalRead, nBlobSize);

    if(ePieceType == SA_LastPiece || ePieceType == SA_OnePiece)
	{
		fclose(pFile);
		pFile = NULL;
	}
}
Posted
Updated 31-Mar-12 2:57am
v2
Comments
Gbenbam 31-Mar-12 10:57am    
Can anyone explain the use of colon before values in the second code i.e //(I don't understand this line)// Insert 2 rows cmd.setCommandText( "Insert into test_tbl(fid, fvarchar20) values(:1, :2)");

1 solution

First code sample creates a table named "test_tbl" in Oracle database with "Create Table" SQL command. The table consists of three columns named-
fid, fvarchar20, fblob

with types -
integer, varchar(20), blob

For details see:Oracle commands[^]
Blob - "Oracle supports over 20 data types, and you can even define your own custom data types. For large objects, Oracle offers the BLOB, CLOB and LONG RAW (obsolete)."How to read a BLOB[^]
It then writes in first two columns of test_tbl -
1, 'Some string (1)'

Every command is executed with "Execute". And finally, changes are committed on success.
 
Share this answer
 
Comments
Gbenbam 31-Mar-12 10:30am    
Can you explain the use of colon before values in the second code i.e
//(I don't understand this line)// Insert 2 rows
cmd.setCommandText(
"Insert into test_tbl(fid, fvarchar20) values(:1, :2)");
Sergey Chepurin 31-Mar-12 10:57am    
http://courses.utas.edu.au/help/wvtwbind.htm

"What are bind variables?
You can add bind variables to a hand-coded SQL query when creating components such as charts, reports, calendars, frame drivers, and dynamic pages. Each bind variable corresponds to a column in the table or view on which the component is based and creates an entry field in the customization form for the component. Users can enter values in the entry fields to select the column data to display in the component.
Syntax -
A bind variable appears in a SQL query as an alphanumeric string preceded by a colon (:var1, :var2, :var3,&ldots;)."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900