[Python] 백슬래시(역슬래시) 입출력
백슬래시(\)로 insert 오류
쿠키값을 db에 저장해야할 일이 생겼는데, 쿠키값에 백슬래시(\)가 들어있어서 insert시에 공백으로 입력되는 문제가 발생하였다.
\를 \\로 입력하면 되는데 !! python 에서 어떻게 하는지 잘 모르겠어서 삽질을 했다..ㅠ
\ => \\ 로 바꾸고 싶었는데 입력 자체가 안되더라💦
코드 작성
php 에서는 내장함수은 addslashes()가 있어서 사용하면 편리했는데, 파이썬도 분명 있을 것 같은데 못 찾았다..😯
아래 처럼 작성해보았는데 역시나 안된다..😮💨
(코드는 일단 막 적고 보는 편)
일단 급해서 임의로 코드를 작성했는데.. 더효율적인 방법 알고 계시다면 알려주시면 감사하겠습니다..ㅎ
single_backslash = "\\"
double_backslash = "\\\\"
cookie = cookie.replace(single_backslash,double_backslash)
생각해보니 굳이 변수에 담을 필요 없을 것 같아서 빼버렸다
간단한건데 이게 뭐라고 붙들고 있었을까 ?,,ㅎ😇
#20230111 추가
관련 자료를 찾아서 업데이트 합니다.
관련 자료 (stackoverflow)
파이썬에서 따옴표 앞에 \ 백슬래시 삽입하기
https://stackoverflow.com/questions/44820628/how-to-insert-backslash-before-quotes-in-string-python
how to insert \ backslash before quotes in string python
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...
stackoverflow.com
MySQL용 이스케이프 문자열(python)
https://stackoverflow.com/questions/3617052/escape-string-python-for-mysql
Escape string Python for MySQL
I use Python and MySQLdb to download web pages and store them into database. The problem I have is that I can't save complicated strings in the database because they are not properly escaped. Is t...
stackoverflow.com
pymySQL
https://stackoverflow.com/questions/32933025/python-pymysql-sending-a-string-with-quotes
Python pymySQL sending a string with quotes
I'm trying to insert a string to a mySQL database like this: cur.execute('UPDATE connections SET cmd = "' + command + '", client_new = 1 where ip = "' + ip + '"') When I set command to be echo "h...
stackoverflow.com
오류 발생 코드
cur.execute('UPDATE table_name SET value1 = "' + a + '", value2 = 1 where value3 = "' + b + '"')
자리 표시자(placeholders) 사용
자리 표시자를 사용하면 mysql 드라이버가 쿼리를 이스케이프한다.
cur.execute('UPDATE table_name SET value1=%s, value2=1 where value3=%s', (a, b))
MySQLdb User's Guide
https://mysql-python.sourceforge.net/MySQLdb.html
MySQLdb User's Guide
If you want to write applications which are portable across databases, use MySQLdb, and avoid using this module directly. _mysql provides an interface which mostly implements the MySQL C API. For more information, see the MySQL documentation. The documenta
mysql-python.sourceforge.net