This question already has answers here:
Escaping chars in Python and sqlite
(4 answers)
Closed 4 years ago.
So currently my code is this which works
result = connection.execute('SELECT * FROM PastOrders WHERE CustomerID="1";')
But as others use my programme the customer will change so how do I replace 1 with a variable like this..
cat = str(1)
result = connection.execute('SELECT * FROM PastOrders WHERE CustomerID=cat;')
However, this doesn't work so any help thanks
Have you considered breaking the SQL statement.
cat = '\"+'str(1)+'\"';
result = connection.execute('SELECT * FROM PastOrders WHERE CustomerID='+cat+';)'
Related
This question already has answers here:
Evaluating a mathematical expression in a string
(14 answers)
Closed 7 months ago.
How to make a function that can interpret a string as a code ?
var1 = 5
var2 = 10
var3 = 7
str = "(var1*var2)+var3"
result = calculate(str) # I need help to write this function
result should output 57
Use result = eval(str) instead of result = calculate(str).
Disclaimer: eval() should be used carefully since it can execute anything passed to it. More details can be found in this article.
This question already has an answer here:
CS50: LIKE operator, variable substitution with % expansion
(1 answer)
Closed 3 years ago.
I'm trying to do a wildcard search to find values that have the string of a variable in any position, so searching like %this%, but I just can't work out what to enclose in %%. I've tried all kinds of combinations with single quotes and % in different places.
variable = request.form.get("input")
results = db.execute("SELECT * FROM table WHERE column LIKE :placeholder", {"placeholder": variable}).fetchall()
This will work:
variable = request.form.get("input")
variable = f"%{variable}%"
results = db.execute("SELECT * FROM table WHERE column LIKE :placeholder", {"placeholder": variable}).fetchall()
This question already has answers here:
Remove 'u' from a python list
(5 answers)
Closed 5 years ago.
i saw that this question already been asked but the solutions i saw didn't worked for me.
i have a python script with tuple list which i want to retrieve without the unicode char (u') and without any other chars (like < [] > or < ' >) so only the data will pass to parameter.
my code look like this -
sql_cursor = con.cursor()
cursor = con.cursor()
cursor.execute(get_alerted_ip)
Results = cursor.fetchall()
for row in Results:
ip_to_open.append(row)
print (row)
con.close()
the output is -
(u'172.1.1.124',)
and the wanted output should look like -
172.1.1.124
I have already tried to convert the tuple to list or string in order to use methods like replace but it's not working.
what am i doing wrong? please help.
Simply
row[0].encode("utf-8")
will return the ascii version of the string
This question already has answers here:
What does % do to strings in Python?
(4 answers)
Closed 8 years ago.
def getresults(candidates, parties, votes, percentageofvote):
results = "/=\/=\=/=\=/=\=/=\=/=\Election Results/=\=/=\=/=\=/=\=/=\=/=\=/=\/\n\n Candidate Party Votes Percent\n\n\n"
for x in range(len(candidates)):
results = results + "%12.12s"%candidates[x] + "\t%3.3s"%parties[x] + "\t%3.3s"% votes[x] + "\t" + str(percentageofvote[x]) + "\n"
return(results)
So my question is what the %12.12, and other percents are doing in this code. I understand everything else about the code, what its doing etc but I don't quite understand what the percent does. I know that normally it works as a modulus and will tell you the remainder between two numbers being divided. Clarification on this would be much appreciated.
It does formatting of strings.
More info on it:
http://docs.python.org/2/library/string.html#format-examples
This question already has answers here:
What is the difference between __str__ and __repr__?
(28 answers)
Closed 7 months ago.
In Python 2.6, I need to create a string by concatenating INTRANET\ and a userid such as jDoe to obtain a string INTRANET\jDoe. This will string will be a part of a SQL query. I have tried this a number of ways but end up getting INTRANET\\jDoe and hence my query does not return any results.
I want to do this:
a = 'INTRANET\\'
b = 'jDoe'
c = a+b ### want to get c as 'INTRANET\jDoe', not 'INTRANET\\jDoe'
Thanks
The problem seems a little different:
When I print c, I get 'INTRANET\jDoe'. But when I append c to a list (to be used in a sql query) as below:
list1 = []
list1.append(c)
print list1
>>>['INTRANET\\jDoe']
Why is this ?
The additional \ is there due to python escaping.
>>> print 'INTERNET\\jDoe'
INTERNET\jDoe
It doesn't affect the SQL you are using. You should look at another direction.
Try the following code,
s1 = "INTRANET"
s1 = s1 + "\\"
s1 = s1 + "jDoe"
print s1
This will give the correct output INTERNET\jDoe
If you simply try to view the content of the variable you will see an extra \, which is an escape sequence in python. In that case it will show,
'INTERNET\\jDoe'