Add simple quote in sql request [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I currently work on a project in python and I would have liked to make a SQL query with an apostrophe in my character string. Ex: "Jimmy's home."
And I have this error:
1064 You have an error in your SQL syntax
So, I've tried to put a \ before the apostrophe, but when I look at my database, just the strings before the \' are in my field.
String to in query: "Jimmy\'s home"
String in Database: "Jimmy"
I don't understand why?

As has been posted here and elsewhere many many times before, you must not use string interpolation to build up SQL strings. Use parameters intead:
REQ = u"INSERT INTO organismes (NAME_organisme`,ID_organisme,adresse,cp,town,lat,lon,tel,fax,‌​email,website) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
cursor.execute(REQ, (NAME,ID,LIGNE,CP,TOWN,LAT,LON,TEL,FAX,MAIL,URL))
Note, no quotes around the placeholders in the query string: the db adapter puts them in for you as necessary.
Also, please don't use capital letters for things that are not constants.

You need use "Jimmy''s" to get your result"Jimmy's" instead of "Jimmy\'s" in SQL.

because ' or " is a special key, when you put it in your query the sql will automatically find where is the other ' or " that's why there's what we called a escape keycode which is \ the use of this is to display the next character in original value

Related

I want to make \ into a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Edit= Some moderators recommended me to make my self more clear, so here we go.
As a personal project in python, I'm making a very simple software that asks the user for an email address and then checks if the syntaxis of the email is correct.
I made a tuple of special characters that are not allowed in an email address, one of those characters is "\". I was looking online like crazy for how to make \ into a str with no result. I try looking online for the use of the function \ with no result either.
V = "\" doesn't work, it gives me a syntax error. I know it is possible to make it into a string because I've done it with an Input() command.
Please help.
It's not clear to me what language you're using - but in most cases you need to escape the backslash, as it is an escape character itself.
V="\\"
This functionality exists that you can include special characters (in this case, a double quote) in the string:
V="The following will be in quotes: \"Hello, World\""
In this case, the escaped double quotes will be treated as literal characters in the string, and will not signal the end of the string as they would without the escape character.

Python - 're.sub'ed string still contains special characters when put into sqlalchemy db.execute command [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
In the search route of my flask webapp, I want to remove special characters from the user input (q) before searching it up in my PostgreSQL database:
q = re.sub('[?*()+[]+', '', q)
search_results = db.execute("SELECT name WHERE name SIMILAR TO :q",{"q": q+'%'})
Unfortunately something still seems to go wrong, and I keep getting a DataError from sqlalchemy. E.g. when I input '?', I get:
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidRegularExpression) invalid regular expression: quantifier operand invalid
[SQL: SELECT name WHERE name SIMILAR TO %(q)s]
[parameters: {'q': '?%'}]
(Background on this error at: http://sqlalche.me/e/9h9h)
The parameters suggest that the value of q is still '?', but when I try removing special characters with re.sub() in the terminal, it successfully removes any special character I want:
>>> import re
>>> q = '?'
>>> q = re.sub('[?*()+[]+', '', q)
>>> q
''
Besides that, queries with accepted characters are successfully searched up.
Where lies the problem and how do I fix this?
Rebooting the flask server fixed it, even though I normally don't need to do this after changes.

How to extract groups contains desired string from between quotes using regex? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I would like to extract some strings from between quotes using regular expression. The text is shown below:
CCKeyUpDomReady('test.asmx/asdasd', 'QMlPJZTOH09XOPCcbB2jcg==', '0OO6h+G2Tzhr5XWj1Upg0A==', '0OO6h+G2Tzhr5XWj1Upg0A==', '/qqwweq2.asmx/qqq')
Expected result must be:
test.asmx/asdasd
/qqwweq2.asmx/qqq
How can I do it? Here is the platform for testing:
https://regexr.com/3n142
The criteria: string which is between quotes must contains "asmx" word. The text is much more than showed above. You can think like that you are searching asmx urls in a website source code.
See regex in use here
'((?:[^'\\]|\\.)*asmx(?:[^'\\]|\\.)*)'
' Match this literally
((?:[^'\\]|\\.)*asmx(?:[^'\\]|\\.)*) Capture the following into capture group 1
(?:[^'\\]|\\.)* This is a beautiful trick gathered from PhiLho's answer to Regex for quoted string with escaping quotes. It matches escaped ' or any other character.
asmx The OP's search string/criterion
(?:[^'\\]|\\.)* This again
' Match this literally
The result is in capture group:
test.asmx/asdasd
/qqwweq2.asmx/qqq

How do I escape '\x' in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using pymysql to query a database that has an entry like 'name':'Te\xtCorp', it's a name that I need to preserve. I'm sending it somewhere else with json.dumps() and when it hits this it fails to escape the \x.
What's the proper way to escape the \x without double escaping everything else?
Two options here:
You escape the backslash, like:
'Te\\xtCorp'
You can use a raw string:
r'Te\xtCorp'
Both generate:
>>> 'Te\\xtCorp'
'Te\\xtCorp'
>>> r'Te\xtCorp'
'Te\\xtCorp'
Or printed:
>>> print(r'Te\xtCorp')
Te\xtCorp
Note that in order to inspect the content of the string, you should use a print(..) statement, otherwise you get the repr(..)esentation of that string. For example:
>>> print(json.dumps(r'te\xt'))
"te\\xt"
>>> print(json.loads(json.dumps(r'te\xt')))
te\xt
As one can read in the documentation on String literals:
\xhh...: ASCII character with hex value hh...
So it is used to encode any ASCII character, by specifying the code as a hexadecimal value.

Getting rid of square brackets on either side of a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am importing currency exchange rates from a website. All is well, except python prints the required data as follows:
['12.3098']
which means I can't use the data that I scraped in any calculations in my program. How do I get rid of the square brackets so that I can convert the string inside the square brackets to a float?
you have a list which is not a string... so if you want to get that value out select it like so
float(result[0])
replace 'result' with whatever your object is, aka what you printed ['12.3098']
try printing the type of your object type(result) and if its a list then this will fix your problem
if the type is a string you can do a literal evaluation of it like this
import ast
result = ast.literal_eval(result)
print result[0]

Categories

Resources