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

  • SEARCH
  • Home
  • 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 7935081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:48:53+00:00 2026-06-03T21:48:53+00:00

I am looking for a nice pythonic and SQL-Injection-free solution for a problem with

  • 0

I am looking for a nice “pythonic” and “SQL-Injection-free” solution for a problem with reserved words in MySQL.

I have the following code:

alter_sql = 'ALTER TABLE %s ADD COLUMN %s TEXT'      
cursor.execute(alter_sql, sql_params)

The problem occurs when column name is something like ‘index’, ‘int’, ‘limit’ ….

In MySQL shell I can do:

ALTER TABLE test ADD COLUMN test.limit;

or

ALTER TABLE test ADD COLUMN `limit`;

but not

ALTER TABLE test ADD COLUMN 'test.limit';

How can I achieve that with Python and MySQLdb?

  • 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-06-03T21:48:55+00:00Added an answer on June 3, 2026 at 9:48 pm

    Maybe this will work:

    alter_sql = 'ALTER TABLE `%s` ADD COLUMN `%s` TEXT'
    

    UPDATE: This seems not to work, because binding parameters this way will add single quotes, as MySQLdb supposes this is a string literal.

    Alternatively, you can append the table name before the column name?

    table_name = MySQLdb.escape_string(table_name)
    escaped_column_name = MySQLdb.escape_string(column_name)
    column_name = '`%s`.`%s`' % (table_name, escaped_column_name)
    
    alter_sql = 'ALTER TABLE %s ADD COLUMN %s TEXT' % (table_name, column_name)
    

    This way, you must escape them manually, to avoid binding them and adding single quotes around it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a nice tight regex solution to this problem. I'm looking to
I am looking for nice (java) code generation engine. I have found cglib but
Context The following code produces a nice looking Hello World graphics.drawString(Hello World, 30, 30);
library(ggplot2) This code produces a nice looking plot: qplot(cty, hwy, data = mpg, colour
We've all seen that stores have nice-looking pricing on their products; 1.99, 209.90 and
I have a nice looking slideup/slidedown jquery form on my website. It sends the
This is my code for a nice looking form: <dl> <dt class='breed'><label for=nwberichten>Fieldname</label></dt> <dd>
I'm looking for nice pythonic way of filtering one list by another stop-list, but
I have created a nice looking toolbar using qt Designer and populated it with
I have a CSS class that makes my links look like nice looking buttons.

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.