开发者

Python/Mysql: Refuses to escape string!

开发者 https://www.devze.com 2023-03-11 06:14 出处:网络
I have a pretty long query i need to execute, and i\'m using prepared statements to insert the values to the query. As far as i know, it\'s suppose to work, but it\'s not working!

I have a pretty long query i need to execute, and i'm using prepared statements to insert the values to the query. As far as i know, it's suppose to work, but it's not working!

Here's the code:

    cursor = connection.cursor()
    rows = cursor.execute(r"""SELECT... %s.. %s.. %s...""",(val1,val2,val3,))
    rows = cursor.fetchall()
    rows = text_position(rows)

If i insert a simple-quote it breaks, and same thing if i insert any non-english characters. It works with double-quotes as i wrapped th开发者_开发知识库e statement in them. Any suggestions?


This is a general technique for debugging SQL (especially when generated from another language, like Python):

Stop the mysql server (assuming you run mysqld without logging):

sudo invoke-rc.d mysql stop

Run mysqld with logging turned on:

sudo mysqld --log=/tmp/myquery.log

Run your script.

Look in /tmp/myquery.log for the SQL command that MySQLdb sent to mysqld.

This might give you enough of a clue to solve the problem, otherwise, please post the complete SQL command in Python

cursor.execute(r"""SELECT... %s.. %s.. %s...""",(val1,val2,val3,))

and the corresponding SQL as seen by mysqld in /tmp/myquery.log.

Once you are done debugging, you can shutdown the logging mysqld and restart it with:

sudo invoke-rc.d mysql restart
0

精彩评论

暂无评论...
验证码 换一张
取 消