请我在访问数据库方面需要帮助 [英] Please I need help with accessing databases
问题描述
我要求提供的材料将帮助我从使用C/C ++ i/o迁移到使用SQL Server,Oracle等数据库.我得到了一些很好的材料.但是我有
在理解某些代码时遇到问题.下面显示了一些代码示例.除了声明的代码行外,一切对我来说都是清晰易懂的.什么是fblob.什么是test_tbl.
谁能向我解释规定的代码行.
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; // 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;
}
}
推荐答案
第一个代码示例使用创建表" SQL命令在Oracle数据库中创建一个名为"test_tbl"的表.该表由名为-
的三列组成
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
有关详细信息,请参见: Oracle命令 [ ^ ]
Blob-"Oracle支持20多种数据类型,您甚至可以定义自己的自定义数据类型.对于大型对象,Oracle提供了BLOB,CLOB和LONG RAW(已淘汰)." dba-oracle.com/t_read_blob.htm>如何读取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)''
每个命令都通过"Execute"执行.最后,成功就做出了改变.
Every command is executed with "Execute". And finally, changes are committed on success.
这篇关于请我在访问数据库方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!