Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 943653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:28:07+00:00 2026-05-15T22:28:07+00:00

There are many escape functions in the Python module mysqldb whose documentation I don’t

  • 0

There are many escape functions in the Python module mysqldb whose documentation I don’t understand, and my efforts at looking them up have revealed nothing.

>>> print _mysql.escape.__doc__
escape(obj, dict) -- escape any special characters in object obj
using mapping dict to provide quoting functions for each type.
Returns a SQL literal string.

This documentation page says the same thing. But what’s supposed to be in that “mapping dict”? I tried a couple of (mostly random) things and only go errors back. What’s even more frustrating is that, while the escape_string() method works, its documentation string is:

>>> print _mysql.escape_string.__doc__
escape_string(s) -- quote any SQL-interpreted characters in string s.

Use connection.escape_string(s), if you use it at all.
_mysql.escape_string(s) cannot handle character sets. You are
probably better off using connection.escape(o) instead, since
it will escape entire sequences as well as strings.

So, I am better off using _mysql.escape(), am I? Well, uh… okay, but how? What on earth is that “mapping dict”? PHP, in that way at least, was a lot less cryptic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:28:08+00:00Added an answer on May 15, 2026 at 10:28 pm

    I learned this by looking in /usr/lib/pymodules/python2.6/MySQLdb/connections.py
    to see how it called connection.escape. A little sniffing around leads to
    MySQLdb.converters.conversions. Here is a snippet:

    {0: <class 'decimal.Decimal'>,
     1: <type 'int'>,
    ...
     <type 'dict'>: <built-in function escape_dict>,
     <type 'NoneType'>: <function None2NULL at 0xae9717c>,
     <type 'set'>: <function Set2Str at 0xae9709c>,
     <type 'str'>: <function Thing2Literal at 0xae971b4>,
     <type 'tuple'>: <built-in function escape_sequence>,
     <type 'object'>: <function Instance2Str at 0xae971ec>,
     <type 'unicode'>: <function Unicode2Str at 0xae9710c>,
     <type 'array.array'>: <function array2Str at 0xae9725c>,
     <type 'bool'>: <function Bool2Str at 0xae97294>}
    

    You can use it like this:

    import MySQLdb
    import MySQLdb.converters
    import datetime
    
    now=datetime.datetime.now()
    connection=MySQLdb.connect(
        host=HOST,user=USER,passwd=PASS,db=MYDB)
    print(connection.escape((1,2,now),MySQLdb.converters.conversions))
    # ('1', '2', "'2010-07-24 19:33:59'")
    

    PS. Regarding Bobby Tables: For normal use of MySQLdb, you don’t have to manually escape arguments. Just use parametrized arguments when calling cursor.execute, and MySQLdb will automatically quote the arguments for you.

    For example:

    sql='insert into students (name,grade,date) values (%s, %s, %s)'
    args=("Robert'); DROP TABLE Students; --",60,now)   # no manual quotation necessary
    cursor=connection.cursor()
    cursor.execute(sql,args)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 524k
  • Answers 524k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is several things at play in your example. Here… May 16, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer The Zend_Mail_Storage_Imap will not return Zend_Mail but Zend_Mail_Message. So, you'll… May 16, 2026 at 9:56 pm
  • Editorial Team
    Editorial Team added an answer this is the correct way to add css & javascript… May 16, 2026 at 9:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

There are many legends about them. I want to know the truth. What are
There are many functions (especially in the POSIX library) that return pointers to almost-necessarily
I've tried many functions already, but I simply can't figure this out. The right
First and foremost, are there many android developers here? Is this a good place
There are many tables nested inside as it showd in the following three images.
There are many how-to's for people who want to use Rails with Apache and
There are many, many, many options out there, but I still couldn't settle with
Many product developers want to write a .NET application which will work seamlessly with
According to the jQuery docs, I need to escape metacharacters that occur in my
I am creating a forum software using php and mysql backend, and want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.