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 8862023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:37:40+00:00 2026-06-14T15:37:40+00:00

I have a small problem. And i think I am f=doing something silly but

  • 0

I have a small problem. And i think I am f=doing something silly but can not figure it out

first :
i want to fire a dynamically constructed sql on mysql from python. Its simple insert statement but the values can have double quotes and other special characters in them. So, i was not sure how to do this.
Then i came accross this thread – Stackoverflow

according to this i tried the following code

def fire_statement(stmt, value_tuple=None):

    try:
        if value_tuple == None:
            cur.execute(stmt)
            return True
        else:
            v = tuple(value_tuple)                
            cur.execute(stmt % v)
            return True
    except Exception, ex:
        print ex
        print "Query: '"+stmt+"'"
        return False

I have to pass the dynamically created stmt and value_tuple in this function i have no other way as the column names and values both will depend on external conditions. qand one more piece of information is the passed value_tuple is originally a list.

Two conditions are occurring in this code, and alas none of the approaches work

condition-1: when value_tuple = (‘269’, ‘1’, ’69’, ‘1096’, ‘1’, ‘3070801’, ‘BE’, ‘1963’, ‘6’)
and stmt = INSERT INTO XXX (NCLASS, CRECEIVE, SUBCAT, GDATE, ASSCODE, PATENT, COUNTRY, GYEAR, CAT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
i am getting this error – not all arguments converted during string formatting

(obviously the line – cur.execute(stmt % v) is changed to cur.execute(stmt , v)) for this case.

condition-2: when value_tuple = (‘269’, ‘1’, ’69’, ‘1096’, ‘1’, ‘3070801’, ‘BE’, ‘1963’, ‘6’) and stmt = INSERT INTO XXX (NCLASS, CRECEIVE, SUBCAT, GDATE, ASSCODE, PATENT, COUNTRY, GYEAR, CAT) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
it is throwing this error – (1054, “Unknown column ‘BE’ in ‘field list'”)
Query: ‘INSERT INTO XXX (NCLASS, CRECEIVE, SUBCAT, GDATE, ASSCODE, PATENT, COUNTRY, GYEAR, CAT) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)’

it seems that it is not replacing at all. but if i do a print (stmt % v) just before the cur.execute then the result shows as – INSERT INTO XXX (NCLASS, CRECEIVE, SUBCAT, GDATE, ASSCODE, PATENT, COUNTRY, GYEAR, CAT) VALUES (269, 1, 69, 1096, 1, 3070801, BE, 1963, 6)

Which is again obviously not right because it is not enclosing the string value within quotes. And i believe that this may be causing the error. for this i am badly stopped at the development cycle for a few hours now. Please help

Thanks in advance

  • 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-14T15:37:42+00:00Added an answer on June 14, 2026 at 3:37 pm

    You want to pass the arguments using

    cur.execute(stmt, v)
    

    and not

    cur.execute(stmt % v)
    

    If you have

    "INSERT INTO tab (col1, col2) VALUES (%s, %s)"
    

    and you pass two strings ('val1', 'val2') using %, it will become:

    "INSERT INTO tab (col1, col2) VALUES (val1, val2)"
    

    which is interpreted as column names, which obviously do not exist.

    If you do

    cur.execute(stmt, v)
    

    it will be interpreted correctly as

    "INSERT INTO tab (col1, col2) VALUES ('val1', 'val2')"
    

    You can also use

    print cur.mogrify(stmt, v)
    

    to see the correctly formatted query as it could be executed in your DB.

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

Sidebar

Related Questions

I have a small problem which i can't seem to figure out. I tried
I have a small problem and I've figured out where when and why it
I have a small problem which i can't seem to solve myself. Look at
I have a small problem where I want to find the next active item
I am fairly new to Yii and I have a small problem, but cannot
have small problem, and would very much appreciate help :) I should convert byte
I have small problem with Spring MVC. Basically what I'm trying to do is
I have a small problem with the visibility of an ellipse. In runtime I
I have a small problem with Smarty... I have two different template files in
i have a small problem, i am creating a edit page in my asp.net

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.