What does %%%s%% mean? Also what does `%(author) mean?
cursor.execute("SELECT * FROM new WHERE author LIKE '%%%s%%' "%(author)
In a SQL LIKE pattern, % matches any sequence of characters. So if you write:
WHERE author LIKE '%Jones%'
it will match an author that contains Jones anywhere in it.
This code is also using the Python % operator for string formatting, that's what %(author) is for. This formatting operator looks for formatting specifications in the string that begin with % -- %s means to substitute the value of the corresponding string from the tuple (author).
And since % has special meaning in the format string, you need to double it to produce a literal % character.
So if you do:
author = "Jones"
then the value of
"SELECT * FROM new WHERE author LIKE '%%%s%%' "%(author)
will be:
"SELECT * FROM new WHERE author LIKE '%Jones%'"
Related
In my sql code, I receive a input string as a regular match's filter, I want to have the whole string as a normal string, even it includes some special characters.
Just look below:
do $$ declare
jdata jsonb='[{"name":"Dog 3*240+1*120"}]'::jsonb;
vfilter1 text='dog';
vfilter2 text='3*240+1*120';
vexists bool=false;
begin
select jdata #? concat('$[*] ? (#.name like_regex "',vfilter1,'" flag "i")')::jsonpath into vexists;
raise notice 'exists:%',vexists; --the result is true
select jdata #? concat('$[*] ? (#.name like_regex "',vfilter2,'" flag "i")')::jsonpath into vexists;
raise notice 'exists:%',vexists;-- the result is false
end;
$$ language plpgsql;
the string 3*240+1*120 include + and * characters, perhaps this causes the regular match have them as special character. In my code, I just want to have the whole vfilter string includes all special characters together as a normal string for the regular match.
What should I do?
You should read the documentation for the feature you are using.
The optional flag string may include one or more of the characters i
for case-insensitive match, m to allow ^ and $ to match at newlines, s
to allow . to match a newline, and q to quote the whole pattern
(reducing the behavior to a simple substring match).
I am new to python and currently learning to use SQL with python. I have the following code:
word = input("Enter a word: ")
query = cursor.execute("SELECT * FROM Dictionary WHERE Expression LIKE '%s%' " % word)
results = cursor.fetchall()
The second line throws an error since I don't think I can use '%s%' like that? How would I change this so as to be able to make this work? I want to be able to return all related entries to the users input. So if the user inputs "rain", then I want the query to return all possible results e.g. "raining", "rainy" etc. Thank you.
You can try
query = cursor.execute(f"SELECT * FROM Dictionary WHERE Expression LIKE '%{word}%' ")
You should use cursor.execute() parameter substitution rather than string formatting, to prevent SQL injection.
Then use CONCAT() to surround the search string with %.
query = cursor.execute("SELECT * FROM Dictionary WHERE Expression LIKE CONCAT('%', %s, '%' "), (word,))
I can't understand this:
[lsort [::array names my_array *,dut_inst]]
What meaning of * ?
What meaning of dut_inst ?
Where can I read about it ?
Let's assume:
my_array(0)=0
my_array(1)=1
my_array(2)=2
What will I get ?
That optional argument to array names is an optional glob pattern that is used to filter the results to return down to a subset. The rules for how it works are described in the documentation for string match, but in the case of *,dut_inst we have two parts:
* matches any number of characters.
,dut_inst is literal (as none of the characters in it are special in the string match rules).
The effect is to return a list of all element names whose names end with ,dut_inst. With your sample data, you get an empty list. With this sample data:
my_array(foo,bar)=1
my_array(boo,dut_inst)=2
my_array(dut_inst,grill)=3
my_array(abc,dut_inst,def)=4
my_array(pqr,dut_inst)=5
You'd get this output (assuming the lsort is there; Tcl does not guarantee the order of array iteration): boo,dut_inst pqr,dut_inst
While using Tcl, as mentioned in the documentation here, shouldn't the following code,
string match h* match
, return 1 for the matched h character in "match" instead of what it actually returns , that is, 0 ?
In the same page itself you have the following content,
# Matches
string match f* foo
# Matches
string match f?? foo
# Doesn't match
string match f foo
The match is applied as if a whole word, not like string contains that particular word.
With string match h* match, it will try to match a pattern whose first letter is h and further zero or more occurrence of any string of characters, which is not true for word match.
Instead, you can rely on regexp for what you expect to happen.
# Matches, will return 1
regexp h* match
In a C# project, Regex is behaving weirdly for me.
I have this method:
string RegTest()
{
string HTML = "<input type=\"hidden\" name=\"authenticity_token\" value=\"d27956cca6b75db4d8dd502d0569dd246455131c\">";
Regex AuthRegex = new Regex(#"name=""authenticity_token"" value=""([A-Ba-b0-9/-]+)""");
string Auth = AuthRegex.Match(HTML).Value;
return Auth;
}
For a reason I don't understand at all, the Regex doesn't find any match with this pattern. It just returns "".
How can I fix this?
The problem is:
[A-Ba-b0-9/-]+
What character ranges (x-y) basically do is get a set of all characters in between. In other words, a-b = all letters between a and b, aka only a and b. However,
d27956cca6b75db4d8dd502d0569dd246455131c
looks like a hex. Therefore, you should use
[A-Fa-f0-9-]+
instead.