how to insert \ backslash before quotes in string

2019-10-09 06:48发布

This question already has an answer here:

I want to insert a \ before each quotes in my string to insert it to my SQL database using pymysql. If I don't escape quotes I can not insert strings in my database.

For exemple:

str = "ok I'm ready"

must be :

str = "ok I\'m ready"

but print str must be : "ok I'm ready"

To perform it I have done:

str = str.replace("'", "\'")

But it's not working and I still can not insert my string in my database. I have the error message :

(1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Semantic Search software can power everything from intuitive chatbots to searc' at line 1"): ProgrammingError Traceback (most recent call last): File "/var/task/setter_organizations.py", line 38, in handler structured_data.insert() File "/var/task/setter_organizations.py", line 107, in insert self.rds.insertItem(self.type, self.data) File "/var/task/RDS/rds.py", line 92, in insertItem return self.insert(req) File "/var/task/RDS/rds.py", line 35, in insert affected_rows = self.cursor.execute(request) File "/var/task/RDS/pymysql/cursors.py", line 166, in execute result = self._query(query) File "/var/task/RDS/pymysql/cursors.py", line 322, in _query conn.query(q) File "/var/task/RDS/pymysql/connections.py", line 856, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "/var/task/RDS/pymysql/connections.py", line 1057, in _read_query_result result.read() File "/var/task/RDS/pymysql/connections.py", line 1340, in read first_packet = self.connection._read_packet() File "/var/task/RDS/pymysql/connections.py", line 1014, in _read_packet packet.check_error() File "/var/task/RDS/pymysql/connections.py", line 393, in check_error err.raise_mysql_exception(self._data) File "/var/task/RDS/pymysql/err.py", line 107, in raise_mysql_exception raise errorclass(errno, errval) ProgrammingError: (1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Semantic Search software can power everything from intuitive chatbots to searc' at line 1")

I also would print my string and don't see the \

Does anyone know how can I do ?

5条回答
beautiful°
2楼-- · 2019-10-09 07:10

You should add a second \:

your_str= your_str.replace("'", "\\'")
查看更多
\"骚年 ilove
3楼-- · 2019-10-09 07:23

This works, but is not what the OP wanted:

str = "ok I'm ready"
str = str.replace("'", "\\'")
print(str)

EDIT:

Please take a look at this question: A good way to escape quotes in a database query string?

And this other one: Escape string Python for MySQL They used:

conn.escape_string()

EDIT 2: Please see this: Python pymySQL sending a string with quotes

and note how they have used placeholders:

cur.execute('UPDATE connections SET cmd=%s, client_new=1 where ip=%s', (command, ip))
查看更多
虎瘦雄心在
4楼-- · 2019-10-09 07:25

You can use double backlash:

str = "ok I\\'m ready"

Edit: Now that you clarify, a way to solve it is json.dumps("I'm")

查看更多
走好不送
5楼-- · 2019-10-09 07:31

if you try this will displayed "ok I\\'m ready" in terminal actually its "ok I\'m ready" you need to print it .

The result '\&' is only displayed - actually the string is \&:

str = "ok I'm ready"
str = str.replace("'", "\\'")

or

str = "ok I'm ready".replace("'","\\'")

do,

print str  # python 2

print(str) # python 3
查看更多
Viruses.
6楼-- · 2019-10-09 07:35

No problem simply use r before text like following:

str = r"ok I\'m ready"

No need to replace.

查看更多
登录 后发表回答