How do I use LIKE with Pro*C? The code below doesn't work. I need to search records in database.
cout<<"Employee name\t\t: ";
cin.getline(name,50);
EXEC SQL SELECT NAME INTO :nameResult FROM EMPLOYEE WHERE NAME LIKE '%:name%';
Declare a host variable like this: "char hLikeVar[64];". Then string copy "%[empl name]%" into it. For [empl name] use the input you got from the user. Then you can do this:
... WHERE NAME LIKE :hLikeVar;
so Pro * C provides varchar structures, where you aren't required to handle many things. So if you are declaring
varchar LikeVar[Length_of_Variable];
and use
strcpy(LikeVar.arr); /* .arr is the character array */
LikeVar.len = strlen(LikeVar.arr);
after this you can use directly: with in the sql statement.
Related
I have a code for deleting table type in MSSQL.Have to convert into PGSQL which is giving error.
Looking forward for a PGSQL code which deletes from a Table Type User defined:
below MSSQL CODE:
declare #Entity TRef_StructureTree readonly //input parameter from procedure
DELETE Tef_StructureTree
FROM Tef_StructureTree
inner join (select * from #Entity) as source
on Tef_StructureTree.ChildCodeBDR=source.ChildCodeBDR AND
Tef_StructureTree.ChildScheme=source.ChildScheme;
Below is definition of USer defined Table type:
CREATE TYPE [dbo].[Tef_StructureTree] AS TABLE(
[ChildCodeBDR] [numeric](10, 0) NOT NULL,
[ChildScheme] [nvarchar](100) NOT NULL,
)
below PGSQL CODE:
CREATE OR REPLACE FUNCTION UpdateStrucTree(v_entity Tef_StructureTree[])
as
begin
DELETE FROM v_entity
where v_entity."ChildCodeBDR" in(select "source"."ChildCodeBDR" from unnest(v_entity) as "source" )
and v_entity."ChildScheme" in (select "source"."ChildScheme" from unnest(v_entity) as "source" );
end;
ERROR: relation "v_entity" does not exist
Please Help by providing the equivalent!!
Your parameter is called v_entitydata not v_entity, so you either need to change your parameter name or the reference to it. You probably want to delete from the table Tef_StructureTree not from the passed array.
Your function's structure is also not valid for Postgres as the function body needs to be provided as a string, e.g. using dollar quoting.
You can simplify the condition, by only using a single sub-query as well.
So putting all that together, the function should look like this:
CREATE OR REPLACE FUNCTION UpdateStrucTree(v_entity "Tef_StructureTree"[])
as
$$
DELETE FROM "Tef_StructureTree"
where ("ChildCodeBDR", "ChildScheme") in (select "ChildCodeBDR", "ChildScheme"
from unnest(v_entity) )
$$
language sql;
i am using Qt and i have written c++ code,i have already connected with sqlite database.i want to insert name in database
std::string name="Hello";
qry.prepare( "INSERT INTO s_no (Name,Status) VALUES ('name','1' )");
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug( "Inserted!" );
but in db i am finding name only , not hello;
please help me..thank you so much in advance
C++ and SQL are two different programming languages, and execute in different environments. This means that C++ objects are not visible in SQL.
In theory, it would be possible to construct the string containing the SQL command so that the value of the name variable is inserted directly into it:
qry.prepare("INSERT INTO s_no (Name,Status) VALUES('" + name + "', '1')"); // don't do this
However, this will blow up if the name contains a quote. Escaping quotes would be possible with additional code, but a better way of getting variable values into an SQL query is to use parameters:
qry.prepare("INSERT INTO s_no (Name,Status) VALUES(?, '1')");
qry.bindValue(0, name);
(This is the only sensible way of using blob values in a query.)
Try making name a QString. You can change std::string to QString by using: QString name2 = QString::fromStdString(name);. Don't forget to include: #include <QString>.
like clause doesn't want to fetching all the results that near to my word, I must write the complete sentence to get it, for instance:
I have the following data in database:
Lion king
lionheart
level completed
good morning
I want to fetch Lion king, and when I write this part of word lion, in normal it must fetch Lion king and lionheart, but in my case does not fetch anything, unless, must write the complete sentence to fetch the data.
I tried to use the following queries:
SELECT * FROM table WHERE name LIKE '%text'
SELECT * FROM contacts WHERE name LIKE '%text' OR name LIKE 'text%' OR name LIKE '%text%' OR name LIKE text
Notice:
I'm use C++ Qt Framework and the following is what I did
qry.prepare("SELECT * FROM table WHERE name LIKE '%:text'");
qry.bindValue(":text", ui->searchBox_txt->text());
qry.exec();
How can I make the query to does the normal behavior ?
:text
is a query parameter whose name is text
':text'
is the string ":text"
Similarly, "%:text" is exactly what it is; your parameter isn't parsed because it's inside of a string.
You need to concatenate the parameter in with your '%' using the concatenation operator (||):
SELECT * FROM table WHERE name LIKE '%' || :text || '%'
Remove the : after the %. Something like this:
SELECT * FROM table WHERE name LIKE '%text%'
Please also take note that the value is case sensitive
can I call a function inside an MySQL Query? To ask more elaborately consider I have a function which returns the account_id of the customer
int return_account_id(){
return (account_id);
}
Now can I call this function inside my query ? Is it possible ?
`resultset = statement->executeQuery("SELECT `account_id`, `acc_name` FROM `account` WHERE `account_id` = "return_account_id()" ");
Why not this:
#include <sstream>
stringstream query;
query << "SELECT * FROM account WHERE account_id = " << return_account_id() << ";";
resultset = statement->executeQuery(query.str());
Not really. Remember, your C++ code is application code. The SQL statement is server code, potentially running on a different machine.
But, the answer is not "No". You can add user defined functions that MySQL knows about. If you need to do this, the place to start is here.
I would like to print my sql table contents and for that reason, I would like to retrieve the column name from the table. One solution I came across was :
SELECT sql FROM sqlite_master WHERE tbl_name = 'table_name' AND type = 'table'
But looks like I will have to parse the results.
Another suggestion was to use:
PRAGMA table_info(table_name);
but the below sqlite page suggests not to use this :
http://www.sqlite.org/pragma.html#pragma_full_column_names
Does there exists any way to achieve this. Also what would be the syntax to use
PRAGMA table_info(table_name);
Above solutions have been taken from here
Since your question is tagged c I assume you have access to the SQLite C API. If you create a prepared statement with one of the prepare_v2 functions that selects from the table you want you can use sqlite3_column_name to get the name of each column.
You can safely use PRAGMA table_info(table-name); since it's not deprecated in any way (yours post links to another pragma).
int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);
If you are using c/c++, you can use the function sqlite3_get_table(db, query, result, nrow, ncol, errmsg);
Make the query as select * from table;
And the first few results result[0], result[1]...... will have the column names.
This setting will toggle showing column names as part of the return for select statements:
.headers on