Does pqxx::pipeline support prepared statement - c++

Can we execute Prepared statement(parametrised query) inside pqxx::pipeline,
we can insert plain sql statement like:
SELECT * FROM table
but can we do something like given below
pqxx::connection c;
c.prepare("SELECT_QUERY", "SELECT * FROM table");
pqxx::work w(c);
pqxx::pipeline p(w);
w.insert("SELECT_QUERY");
w.complete()
Thanks

Related

How to know if function 'callback' in sqlite returned something?

For example my inquiry(question?) in SQL is:
SELECT * from COMPANY where imie="John",surname="Wattson",age=31;
I use sqlite3_exec where one of the arguments is callback. I don't know if this record is in my table, and would like to know it using sqlite_exec.
What should I do?
Sorry for my English. :(
If you just want to see if a record exists in the table, then you could do it with sqlite3_exec() using a callback function like this:
int myCallback(void *pUser, int argc, char **colData, char **colNames) {
int *flag = (int*)pUser;
*flag = 1;
return 1;
}
This works because if there are no records matching the query, then the callback function is not called. By returning 1 instead of 0, we are telling SQLite that we don't want any more rows from the query results.
Then, in the function where you are making the db query:
std::string sql = "SELECT * FROM COMPANY WHERE imie='John' AND surname='Wattson' AND age=31;";
char *pSql = sql.c_str(); // char*'s are better for talking to SQLite, and prior to C++14,
// a std::string is not guaranteed to be sequential in memory,
// so 'sql[0]' may not work right
char *pError = NULL;
int fHasResult = 0;
// db is an already-opened sqlite3*
int result = sqlite3_exec(db, pSql, myCallback, &fHasResult, &pError);
if (result) {
cout<<"Error was: "<<pError;
free(pError);
}
if (fHasResult) {
cout<<"The row exists in the database.";
}
else {
cout<<"The row does not exist in the database.";
}
You could use EXISTS, your query should then look something like this;
SELECT EXISTS (SELECT * FROM COMPANY WHERE imie="John" AND surname="Wattson" AND age=31);
For another example you could take a look at this;
Valid query to check if row exists in SQLite3

How to Compare my sql columns dynamically

I am not able to get idea about the following requirement. The example table follows.
CREATE TABLE `test` (
`Id` INT NOT NULL,
`Name` VARCHAR(45) NULL,
`did_fk` INT NULL,
`adid_fk` INT NULL,
PRIMARY KEY (`Id`));
INSERT INTO test (id,name,did_fk,adid_fk)
VALUES
(1,'Rajesh',1,1),
(2,'Neeli',2,2),
(3,'Satish',3,3),
(4,'Ganesh',4,5),
(5,'Murali',9,10);
Here I need to compare the "id" with _fk columns i.e. did_fk & adid_fk. The "id" should be equal to did_fk & as well as adid_fk. If any of them is not true, then I should get that row.Here I need to get the rows 4 & 5.Since "_fk" columns are not equal to "id" value.Problem is "_fk" columns are not fixed. But "id" name is fixed.
SELECT * FROM `test` WHERE `Id` != `did_fk` OR `Id` != `adid_fk`
If your dynamic columns ends with _fk or some another suffix you can try to create SP like following
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetNonEqualFkValues`(IN tableName varchar(255))
BEGIN
DECLARE c_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE curs CURSOR FOR select column_name from information_schema.columns where column_name like '%_fk';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
SET #q = concat("SELECT * FROM ", tableName, " WHERE 1!=1 ");
get_col: LOOP
FETCH curs INTO c_name;
IF done THEN
LEAVE get_col;
END IF;
SET #q = CONCAT(#q, " OR ", c_name," != id");
END LOOP get_col;
PREPARE stmt1 FROM #q;
EXECUTE stmt1;
END
And then invoke for concrete table like
call GetNonEqualFkValues('test')
The code isn't perfect, but it works for me and I think idea should be clear.

Execute PL/SQL script in C++ using OCCI oracle

I want to run SQL script from a C++ program. my code goes like this:
int main()
{
//.....
sql_stmt = "Insert into t1 values ('qwerty');\nInsert into t1 values ('dothar');"
"//and many more INSERT statements";
sql_stmt = "DECLARE\nrollback_check_counter number;\n"
"BEGIN\n"
"rollback_check_counter :=1;\n"
"SAVEPOINT sp_1;\nIF rollback_check_counter = 1 THEN\n"
"BEGIN\n"+sql_stmt+"EXCEPTION\n"
"WHEN PROGRAM_ERROR THEN\n"
"rollback_check_counter :=0;\n"
"ROLLBACK TO sp_1;\n"
"WHEN OTHERS THEN\n"
"rollback_check_counter :=0;\n"
"ROLLBACK TO sp_1;\n"
"END;\n"
"END IF;\n"
"commit;\n"
"END;";
try
{
Connection *conn = env->createConnection(user,passwd); //error prone
Statement *stmt = conn->createStatement();
stmt->setSQL(sql_stmt);
row_count = stmt->execute(); //stmt->execute(sql_stmt);
Connection::conn->terminateStatement(Statement *stmt);
//con->terminateStatement(stmt);
env->terminateConnection(conn);
Environment::terminateEnvironment(env);
}
catch(SQLException& ex)
{}
//.....
return 0;
}
Although when i run these insert statement only they fairly run well but when i forms a SQL Script structure they seems to fail. I want to do so because i want to implement rollback. What am i missing? Could anyone suggest any alternative to implement it.
There are ; missing after both ROLLBACK TO sp_1

Trying to modify an sqlite3 insert to return the PK. Wasn't sure how to do it with sqlite3 specifically

I was trying to insert into a table some data, but I wasn't sure what flag allows me to return the primary key. I believe I recall MSSQL using RETURNING, and some others tack on RETURNS at the end.
could someone help out with the appending of it?
I'm trying to return TABLEA.a, and my query and design would look something like this:
sqlite3 *db;
sqlite3_open("...",&db);
std::string query;
query = "insert into TABLEA (b,c,d,e) values (#b,\"#c\",#d,#e);";
//^--this needs to be modified.
sqlite3_stmt *sqlstmt;
int rc;
rc = sqlite3_prepare_v2(db, query.c_str(), 01, &sqlstmt, 0);
sqlite3_step(sqlstmt);
int ID;
ID = sqlite3_column_integer(sqlstmt,0);
Have you tried sqlite3_last_insert_rowid ?

SQLite SELECT JOIN and VIEWS in C++

I have a two tables in SQLITE and View where I try to compine these two tables (my idea is to get all the rows from table1 where table1.field1 and table2.field99 have the same value.
Tables and View are Table1,Table2 and View1
View code is "SELECT * FROM Table1 JOIN Table2 ON UPPER(Field1) LIKE UPPER(Table2.Field99)"
If I run
SELECT * FROM View1;
or if I run
SELECT * FROM Table1 JOIN Table2 ON UPPER(Field1) LIKE UPPER(Field99);
from sqlite shell it returns the rows just fine.
But if i try to run those SQL statements in C++ with this code:
if(sqlite3_prepare_v2(database,SQLStr,-1,&stmt,0)==SQLITE_OK){
int cols = sqlite3_column_count(stmt);
printf("Query OK: %s \nColumns: %i\n", SQLStr, cols);
while(1) {
status = sqlite3_step(stmt);
if(status == SQLITE_ROW) {
printf("This is a row!\n");
} else if(status == SQLITE_DONE) {
printf("Rows handled!\n");
break;
} else {
printf("Other status!\n");
break;
}
}
}
It just returns:
Rows handled: SELECT * FROM View1;
Columns: 7
Rows handled!
But it doesn't return any rows like the shell does. (there should be "This is a row!" printed for every row in query. I tried to add table names to queries but no help. I also tried to run for example SELECT * FROM Table1; and then the C++ returns the rows. Is it so that SQLITE in C++ can't handle JOINS or Views?
if u want to collect the rows then use Sqlite3_exec() API or column API.sqlite3_step() only evaluates the prepared statement it does not fetch the results, for that u have to use different api available or u can use sqlite3_exec which is a wrapper.
check the following link
http://www.sqlite.org/c3ref/funclist.html
http://www.sqlite.org/cintro.html
All the best.