This question already has answers here:
C# SQLite Parameterized Select Using LIKE
(2 answers)
Closed 5 years ago.
I am using SQLite in C++ to query a database. I want to use the LIKE operator in a select statement such as
SELECT * FROM table WHERE columnName LIKE '%abc%'
But I need to bind the value of abc in the above statement to query. Usually, for normal statements such as SELECT * FROM table WHERE columnName=#name, we can use query.bind(#name, 'name');
But for the LIKE operator, I am not able to get this working. I am not sure if I should be giving it as '%#name%'? This doesn't work and throws an binding error exception.
Any suggestions will be really helpful since otherwise, I need to go through the whole search result having 100's of rows and check each row for a value from an big string array. The idea is to give the string array element as the binding value for the LIKE operator to search the database and fetch just one row.
Any ideas?
Thanks a lot, in advance.
Esash
You can add the percent signs after binding:
... WHERE Col Like '%' || ? || '%'
If name is a variable, use this:
name = "%" + name + "%";
query.bind("#name", name);
Related
I applied advance filter in PowerBI desktop, It is working. Similarly, I want to apply "Contains" or "In" operator to filter a report using URL Query String Parameter. I want to filter with delimitated value. "eq" operator is working but "in/contains" operator is not working.
I tried like this:
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in |181|"));
I have taken reference from here: https://learn.microsoft.com/en-us/power-bi/collaborate-share/service-url-filters
Can you please correct me on what I am doing wrong here?
UPDATE:
I also tried with round bracket, but still its not working. The column have data with pipes, so I need to include pipe while filtering.
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in (|181|)"));
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in ('|181|')"));
PowerBI column data
IN Operator expects a comma seperated list of primitive values or a expression
eg:
~/Products?$filter=Name in ('Milk', 'Cheese')
~/Products?$filter=Name in RelevantProductNames
~/Products?$filter=ShipToAddress/CountryCode in MyShippers/Regions
~/Products?$filter=Name in Fully.Qualified.Namespace.MostPopularItemNames
It seems like your use case is the first example, but if you don't have multiple values you can simply use the EQ Operator
like: filter=Sheet1/TPId eq |181|
Reference:
https://learn.microsoft.com/odata/webapi/in-operator
https://learn.microsoft.com/power-bi/collaborate-share/service-url-filters
I am trying to insert in database(Oracle) in python with cx_oracle. I need to select from table and insert into another table.
insert_select_string = "INSERT INTO wf_measure_details(PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS) \
select PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS from wf_measure_details_stag where oozie_job_id = '{0}'.format(self.DAG_id)"
conn.executemany(insert_select_string)
conn.commit()
insert_count = conn.rowcount
But I am getting below error. I do not have select parameter of data as data is getting from select query.
Required argument 'parameters' (pos 2) not found
Please suggest how to solve this
As mentioned by Chris in the comments to your question, you want to use cursor.execute() instead of cursor.executemany(). You also want to use bind variables instead of interpolated parameters in order to improve performance and reduce security risks. Take a look at the documentation. In your case you would want something like this (untested):
cursor.execute("""
INSERT INTO wf_measure_details(PARENT_JOB_ID, STAGE_JOB_ID,
MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS)
select PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL,
OOZIE_JOB_ID, CREATE_TIME_TS
from wf_measure_details_stag
where oozie_job_id = :id""",
id=self.DAG_id)
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
I have to prepare strings to be suitable for queries because these strings will be used in the queries as field values. if they contain a ' etc the sql query fails to execute.
I therefore want to replace ' with '' I have seen the code to find and replace a substring with a substring. but I guess the problem is a little tricky because replacing string also contains two single quotes '' replacing one quote ' so when I have to find the next occurance it would encounter a ' which was intentionally replaced.
I am using Sql lite C api and the example query might look like this
select * from persons where name = 'John' D'oe'
Since John Doe contain a ' the query will fail , so I want all occurances of ' in the name to replaced with ''
Any ideas how you guys prepares your field values in query to be used in sql ??? may be it's a basic thing but I am not too smart in C/C++.
your help would be very helpful
Use queries with arguments instead of replacing stuff, which could lead to several problems (like SQL injection vulnerabilities).
MySQL example:
sql::Connection *con = ...;
string query = "SELECT * FROM TABLE WHERE ID = ?";
sql::PreparedStatement *prep_stmt = con->prepareStatement(query);
prep_stmt->setInt(1, 1); // Replace first argument with 1
prep_stmt->execute();
This will execute SELECT * FROM TABLE WHERE ID = 1.
EDIT: more info for SQLite prepared statements here and here.
It depends on the SQL Library you are using. Some of them will have the concept of a PreparedStatement, which you will use question marks in place of the variables, then when you set those variables on the statement, it will internally ensure that you cannot inject sql commands.
Is it possible to do a bulk insert with Sitecore Rocks? Something along the lines of SQL's
INSERT INTO TABLE1 SELECT COL1, COL2 FROM TABLE2
If so, what is the syntax? I'd like to add an item under any other item of a given template type.
I've tried using this syntax:
insert into (
##itemname,
##templateitem,
##path,
[etc.]
)
select
'Bulk-Add-Item',
//*[##id='{B2477E15-F54E-4DA1-B09D-825FF4D13F1D}'],
Path + '/Item',
[etc.]
To this, Query Analyzer responds:
"values" expected at position 440.
Please note that I have not found a working concatenation operator. For example,
Select ##item + '/value' from //sitecore/content/home/*
just returns '/value'. I've also tried ||, &&, and CONCATENATE without success.
There is apparently a way of doing bulk updates with CSV, but doing bulk updates directly from Sitecore Query Analyzer would be very useful
Currently you cannot do bulk inserts, but it is a really nice idea. I'll see what I can do.
Regarding the concatenation operator, this following works in the Query Analyzer:
select #Text + "/Value" from /sitecore/content/Home
This returns "Welcome to Sitecore/Value".
The ##item just returns empty, because it is not a valid system attribute.