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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:25:05+00:00 2026-05-16T07:25:05+00:00

I have below variables try to make an auto generating SQL statement in python

  • 0

I have below variables try to make an auto generating SQL statement in python

_SQL_fields         =   ("ID", "NAME", "BIRTH", "SEX", "AGE")
_add_SQL_desc_type  =   ("PRIMARY KEY AUTOINCREASEMENT",)
_SQL_type           =   ("INT",'TEXT')
_Value              =   (1,'TOM',19700101,'M',40)

bop = ["AND"] * (len(_SQL_fields) - 1) 
mop = ["="] * 5
comma = [","] * 4

exception_field = ["NAME", "BIRTH"]

for successful design a SQL statement I need below strings generated by above variable:

'(ID, NAME, BIRTH, SEX, AGE)'
'(ID PRIMARY KEY AUTOINCREASEMENT INT, NAME TEXT, BIRTH, SEX, AGE)'
'ID = 1 AND NAME = "TOM" AND BIRTH = "19700101" AND SEX = "M" AND AGE = '40''

and removed exception_field strings may looks like

'(ID, SEX, AGE)'
'(ID PRIMARY KEY AUTOINCREASEMENT INT, SEX, AGE)'
'ID = 1 AND SEX = "M" AND AGE = '40''

==========

so far I use something like

str(zip(_SQL_fields,_add_SQL_desc_type,_SQL_type,comma)) 

or using

", ".join(_SQL_fields) 

to get one.

but I get a lot of mess when, especiall in processing the “comma” and “quotes”.

'(ID, NAME, BIRTH, SEX, AGE)'    <===== this is OK for SQL
'(ID, NAME, BIRTH, SEX, AGE,)'    <===== this is NOT

and this

'ID = 1 AND NAME = "TOM" AND BIRTH = "19700101" AND SEX = "M" and AGE = '40'  
                   ^-some var need quotes and some do no need them.

So anyone can share me how to write a clean code for generate above strings?

Thanks for your time!

KC

  • 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-16T07:25:06+00:00Added an answer on May 16, 2026 at 7:25 am

    To obtain

    '(ID PRIMARY KEY AUTOINCREASEMENT INT, NAME TEXT, BIRTH, SEX, AGE)'
    

    you can use izip_longest from itertools module in python.

    Then, to remove exception_fields in the obtain list, you can use set or list comprehension.

    l1 = [1, 2, 3, 4]
    l2 = [2, 3]
    l3 = list(set(l1) - set(l2))
    print l3
    --> [1, 4]
    
    l3 = [val for val in l1 if val not in l2]
    print l3
    --> [1, 4]
    

    I recommend the list comprehension.

    ", ".join(_SQL_fields) 
    

    is perfect to obtain

    'ID, NAME, BIRTH, SEX, AGE'
    

    so i don’t really understand you’re problem.

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

Sidebar

Related Questions

I have below five Integer variables and during the program, they are assigned some
I have this code below. As you can see I am passing two variables
I have a string which has the below content String msg = The variables
I have below code: <a href=# id=@item.Id name=vote ><img src=/Content/images/021.png style=float:left alt= /></a> which
I'm starting out in python.. The details I have written in the below.. It
My problem is that I have to set a variable in a try statement
I have two OptionMenu widgets in the simple pieces of code shown below: variable
I have 2 variable like below char s='s'; char Kaazoooombaa ='s'; how can I
I have a variable that contains an array from form data, seen below: $option1
I have below structure <div class=mybox> <div id=imageslide> <a href=><img src=url /></a> </div> </div>

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.