OCIDefineByPos() with OCINumbers - c++

I'm developing C++ application with OCI. I need to fetch data from the DB in to OCINumber. I'm confused on how to use the function OCIDefineByPos() with OCINumbers.
Can someone help me with this.
Given below is the part of the code where I call OCIDefineByPos function.
pStmt is a OCIStmt* and p_Error is OCIError*. Following function pointers are used.
1. pf_OCINumberFromReal = function pointer to OCINumberFromReal
2. pf_OCINumberToReal = function pointer to OCINumberToReal
3. pf_DefineByPos = function pointer to OCIDefineByPos.
OCINumber ocinTest;
long double dnum = 0.0;
(*pf_OCINumberFromReal)(p_Error, &dnum, sizeof(dnum), &ocinTest);
int iLength1 = sizeof(ocinTest);
OCIDefine* pDfn = NULL;
iRet = (*pf_DefineByPos)(pStmt, &pDfn, p_Error, 1, (dvoid *) &ocinTest,
(sword) iLength1, SQLT_NUM, (dvoid *) 0, (ub2 *)0,
(ub2 *)0, OCI_DEFAULT);
if (iRet == OCI_SUCCESS)
{
(*pf_OCINumberToReal)(p_Error, &ocinTest, sizeof(dnum), &dnum);
std::cout <<std::fixed << std::setprecision (10) << dnum << std::endl;
}
although iRet = OCI_SUCCESS it didn't fetch the value in the db correctly.(Value of the sql query defined using pStmt). dnum is 0.0 even after the call. This pf_DefineByPos is working fine for other data types such as int ,double etc.
So can someone help me to find the issue with this.

If your code is not completely taken out of context, then your missing several essential pieces.
A typical OCI program involves the following steps:
Prepare an SQL statement (OCIStmtPrepare)
Bind the result columns or output parameters to the variables that will receive the result values (OCIDefineByPos)
Execute the statement (OCIStmtExecute)
Do something with the result
It seems to me that you're skipping step 3 and expect some result right after step 2. But step 2 doesn't fetch any data. It just creates an association between the query result and your variables.

You need to DefineByPos for OCINumber with SQLT_VNU type rather than SQLT_NUM.

Related

Try to get blob data from Oracle by OCI routines but get error: ORA-01008: not all variables bound

I try to get blob data from Oracle by OCI routines.
I use the next code but the exceute statement gives ther error: ORA-01008: not all variables bound
What do I wrong? Can anybody help me?
Thanks,
Kees Braaksma
void get_blob_data()
{
// The query
// The results of this methode
// if errstring is empty, the blob data can be found in the 4th parameter.
// otherwise the error is given in errstring
CString csQuery;
csQuery.Format("BEGIN get_blob('%s','%ld',:ERRSTRING,:BLOB); END;", "20", 200);
//init
OCIHandleAlloc((dvoid *)m_OCIEnvironment , (dvoid **)(&m_OCIStatement),
(ub4)OCI_HTYPE_STMT, (size_t)0, (dvoid **)0);
//Prepare statement voor query
OCIStmtPrepare(m_OCIStatement, m_OCIError, (text *)(csQuery),
(ub4)(strlen(csQuery)), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
// output variables
char *pszResult = (char*)malloc(256);
memset(pszResult, 0, sizeof(pszResult));
Blob *blob = new Blob();
m_pIndicator1 = malloc(sizeof(OCIInd));
m_pDefine1 = NULL;
OCIDefineByPos(m_OCIStatement, &m_pDefine1, m_OCIError, (ub4)1,
(dvoid*)*pszResult,
(sb4)256, SQLT_STR,
(dvoid*)m_pIndicator1,
(ub2*)0, (ub2*)0, (ub4)OCI_DEFAULT);
m_pIndicator2 = malloc(sizeof(OCIInd));
m_pDefine2 = NULL;
OCIDescriptorAlloc(m_OCIEnvironment, &blob, (ub4)OCI_DTYPE_LOB, (size_t)0, (dvoid **)0);
OCIDefineByPos(m_OCIStatement, &m_pDefine2,
m_OCIError, (ub4)2,
(dvoid*)blob,
(sb4)-1, SQLT_BLOB,
(dvoid*)m_pIndicator2,
(ub2*)0, (ub2*)0, (ub4)OCI_DEFAULT));
iStatus = OCIStmtExecute(m_OCISrvCtx, m_OCIStatement, m_OCIError, (ub4)1, (ub4)0,
(OCISnapshot *)NULL, (OCISnapshot *)NULL,
(ub4)OCI_DEFAULT);
// results:
// iStatus = -1;
// Errorstring: ORA-01008: not all variables bound
}
You don't have any OCIBindByName() calls. OCIDefineByPos() is used when running SQL queries, which you aren't.
It's odd that you use a mixture of %s and bind variables in the PL/SQL call. I think you want to use bind variables for all parameters.
One handy hint is to see how ODPI-C uses OCI. For LOBs you can get either locators or data directly. The latter is a lot faster, but limited to 1G. It's arguably easiest to install Python and cx_Oracle, and run some samples e.g. ReturnLobsAsStrings.py. You can trace the OCI calls in dpiOci.c.
There are some sample OCI programs (sadly not so accessible - your DBA may be able to install them), look out for cdemolb.c and cdemolbs.c
Best Oracle-users,
Still have problems with getting a blob from Oracle. I use it so less.
Most of the time I copy some code and a new query is working fine.
I asked my collegue to change the function.
New the query is:
select ngm_transactie_pck.get_checkout_blob('20','200') from dual
the result of this function is a blob.
So i programmed:
static OCILobLocator *blob = NULL;
after OCIHandleAlloc() and OCIStmtPrepare() as in previous example i have:
OCIDescriptorAlloc(m_OCIEnvironment, &blob, (ub4)OCI_DTYPE_LOB, (size_t)0, (dvoid **)0);
CIDefineByPos(m_OCIStatement, &m_pDefine2,
m_OCIError, (ub4)1,
(dvoid*)&blob,
(sb4)-1, OCI_DTYPE_LOB,
(dvoid*)m_pIndicator2,
(ub2*)0, (ub2*)0, (ub4)OCI_DEFAULT));
The call to OciDefineByPos() gives crash/stackoverflow.
What do I wrong?
Greetz,
Kees

Can't assert facts in clips embedded application

I'm trying to assert a new fact in CLIPS in embedded application.
I tried two ways:
- The first using assert as in the example in page 74 in the advanced programming guide.
- The second way is using assert-string.
I tried the each way alone and also the two ways together.
I'm using RUN_TIME module. My code outputs the right constructs (defrules and deftemplates) but the new fact is not asserted. Only initial-fact is there. I don't know why!
Here is my code:
#include "clips.h"
int main()
{
void *theEnv, *newFact, *templatePtr;
DATA_OBJECT theValue;
extern void *InitCImage_1();
theEnv = InitCImage_1();
EnvReset(theEnv);
// One way
templatePtr = EnvFindDeftemplate(theEnv, "Navigation");
newFact = EnvCreateFact(theEnv, templatePtr);
if (newFact == NULL) return -1;
theValue.type = SYMBOL;
theValue.value = EnvAddSymbol(theEnv, "Auto");
EnvPutFactSlot(theEnv, newFact, "FlightStatus", &theValue);
EnvAssert(theEnv, newFact);
// The other way
EnvAssertString(theEnv, "(Navigation (FlightStatus Auto))");
EnvRun(theEnv,-1);
EnvListDeftemplates(theEnv, "stdout", NULL);
EnvListDefrules(theEnv, "stdout", NULL);
EnvListDeffacts(theEnv, "stdout", NULL);
}
What is wrong in my code?
Use:
EnvFacts(theEnv,"stdout",NULL,-1,-1,-1);
Rather than:
EnvListDeffacts(theEnv, "stdout", NULL);
Deffacts are constructs that define a list of facts to be asserted when a (reset) command is performed. There is a pre-defined initial-facts deffacts that asserts the (initial-fact) when a reset is performed. That's what you're seeing listed when you call EnvListDeffacts. You want to call EnvFacts instead to see the facts that have actually been asserted (whether created by a deffacts after a reset or directly using assert).

Saving text in Sqlite3, c++

here is my second post in the community, excuse me if I'm forget to add something, just let me know:
I am trying to do a program in c++ able to save text (i want to save code) in a database using sqlite3. Currently I've made a wxWidget program that call some functions from a DLL and this ones interactuate with the database.
The database that I want to make is really simple, it has 3 columns in one table (id,name, ref). My problem comes when I want to save big amount of text that also contains simblos that can conflict with the sql queries (I would like to save files inside the database, for example in the "ref" column ).
I'm using mostly the sqlite3_exec function, because the functions sqlite3_prepare_v2, sqlite_bind, sqlite3_step crash me the DLL where I'm working.
My doubt: Can I directly save any text as big as I want, without taking care about if it has simbols or not? and how can I do it?.
More info: I am working in c++ with code:block(13.12) making a DLL of sqlite3 functions and using MinGW toolchain. (windows 7).
This is an example of an insert function that I'm using:
int DLL_EXPORT add_item(sqlite3* db, string tbname,string col,string item)
{
char* db_err = 0;
if (tbname==std::string()||col==std::string()||item==std::string())
throw std::invalid_argument( "stoi: invalid argument table name");
char buf[200];
sprintf(buf,"insert into %s (%s) values ('%s');", tbname.c_str(), col.c_str(),item.c_str());
int n = sqlite3_exec(db, buf, NULL, 0, &db_err);
dsperr(&db_err);
if( n != SQLITE_OK )
{
//throw something
}
return 0;
}
Thank you in advance.
Thank to CL. for the up commentary
// Add one text to a table
// The column must be specify
//
int DLL_EXPORT add_text(sqlite3* db, string tbname,string col,string id,string item)
{
char* db_err = 0;
if (tbname==std::string()||col==std::string()||item==std::string())
throw std::invalid_argument( "stoi: invalid argument table name");
char *zSQL = sqlite3_mprintf("UPDATE %q SET %q=(%Q) WHERE id=%q", tbname.c_str(),col.c_str() ,item.c_str(),id.c_str());
int n = sqlite3_exec(db, zSQL, NULL, 0, &db_err);
dsperr(&db_err);
sqlite3_free(zSQL);
if( n != SQLITE_OK )
{
// throw something
}
return 0;
}

MySQL call procedure with output parameter from C++ mysqlpp

I need help with the C++ mysqlpp driver calling a stored procedure and retrieving its output parameter. The queries seem to pass successfully but trying to get the stored value causes segmentation fault. My current pseudo code is:
mysqlpp::Connection* connection; // the connection type I am bound to use, no createStatement, prepareStatement methods
Query query = connection->query();
Query transactionQuery = connection->query();
query << "CALL sp_get_transactions_count(" << inputId << ", #transactionsCount);";
transactionQuery << "SELECT #transactionsCount as combinations;";
ClearQuerySentry cleanUpQuery(transactionQuery);
query.exec();
mysqlpp::StoreQueryResult transactionsResult = transactionQuery.store();
if (!transactionsResult || transactionsResult.num_rows() == 0)
{
logWarning(....);
}
else
{
const mysqlpp::Row& transactionRecord = result[0];
environment.pairTransactionsCount = verboseLexicalCast<int>(transactionRecord, "combinations"); // segfault on trying to cast static_cast<const char*>(row[fieldName.c_str()]))
}
I am not very experienced with mysqlpp and MySQL as a whole so it is possible my perception of the solution to be wrong. Thanks in advance.

How Can I Call PL/pgSQL Function From C++ Code

I am trying to call a function which is declared in PostgreSQL with PL/pgSQL. For that I write the code below. My function is working but after that I am taking a "PGRES_FATAL_ERROR". Also when I changed "select removestopwords()" with an sql query like "DELETE * FROM TABLE1" it's working successfully.
I am considering, that error can cause some big problem in future even if now working. How can I call a PL/pgSQL function without taking error?
void removeStopWordsDB(PGconn* conn) {
PGresult *res = PQexec(conn, "select removestopwords()");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printf("removestopwords failed");
cout<<PQresultStatus(res);
PQclear(res);
exit_nicely(conn);
}
printf("removestopwords - OK\n");
PQclear(res);
}
If you get PGRES_FATAL_ERROR from PQresultStatus you should use PQresultErrorField to get all the error data from the result set to provide a useful error message. This will allow you to determine what the actual error is here (quite likely an error being sent over from the server).
Consider creating a class to hold PostgreSQL error details that can be constructed from q PQresult pointer, e.g.:
PgError(const PGresult *rs)
{
severity = GetErrorField(rs, PG_DIAG_SEVERITY);
sqlstate = GetErrorField(rs, PG_DIAG_SQLSTATE);
primary = GetErrorField(rs, PG_DIAG_MESSAGE_PRIMARY);
// ...
}
static std::string GetErrorField(const PGresult *rs, int fieldCode)
{
const char *message = PQresultErrorField(rs, fieldCode);
if (message == NULL) return "";
return std::string(message);
}
Then you can, for example, encapsulate dumping out the error to a stream in this object to provide details just like psql and friends do (although strictly speaking, you'd need the input SQL as well for all of that)
PostgreSQL API doesn't support some flag like "ignore all errors". If you would to ignore result, then just don't check result in host environment. But it is bad strategy.