Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, is it possible that a printed output from an of statement can be placed in an mysql Column. That column num insert the value: you have to pay 5% taxes or you not have to pay taxes.

num = 10
if 5 <= num <= 15:
    print ("you have to pay 5% taxes")
else:
	print ("you not have to pay taxes")

cursor.execute(""
SELECT *, FROM Customers
""")


What I have tried:

I tried to converte the id statement in num= but that doesn’t work.
Posted
Updated 8-Jun-22 5:15am

Not your question, but I suspect an error here :
Your if condition is rather uncommon and unsupported in most languages
Python
num = 10
if 5 <= num <= 15:

Try this instead :
Python
num = 10
if 5 <= num and num <= 15:

My bad.
 
Share this answer
 
v2
Comments
Richard MacCutchan 8-Jun-22 3:54am    
No, the original statement is valid in Python. See 6. Expressions — Python 3.10.5 documentation[^].
Patrice T 8-Jun-22 4:08am    
Thank you for this information.
Richard MacCutchan 8-Jun-22 4:43am    
Don't you just love Python? :)
Patrice T 8-Jun-22 6:52am    
Never used it :)
What statement are you referring to? If you want the printed output to be inserted in your database then you use an INSERT statement not a SELECT. Something like:
Python
statement = "you not have to pay taxes"
num = 10
if 5 <= num <= 15:
    statement = "you have to pay 5% taxes"

cursor.execute('INSERT INTO Customers (tax) VALUES (%s)', statement)
 
Share this answer
 
Comments
Megan van Bommel 8-Jun-22 5:41am    
Yes I use the insert statement down under in my script to insert the value into sql
Richard MacCutchan 8-Jun-22 6:03am    
So what are you saying? Does it work or not? And if not please use the Improve question link above, and add complete details of the code, and what is not working.
Megan van Bommel 8-Jun-22 10:55am    
Yes, the script is working thank you alot. But how can i make this work. Because every date above 2020-03-01 is "voorjaarsvakantie" but it is not a vacation

date_purchase = "2020-0-01"
date_vacation = "Voorjaarsvakantie"
if "2020-02-15" >= date_purchase <= "2020-03-01":
date_vacation = "No vacation"
Richard MacCutchan 8-Jun-22 11:04am    
As far as I can see that is the same issue as the original question. THeonly issue is that you may need to convert the dates into actual date objects, as described at datetime — Basic date and time types — Python 3.10.5 documentation[^].
Megan van Bommel 8-Jun-22 11:05am    
Yes i did that but this an example
# "date" dimension
    date_string = str(dim_date["purchase_date"])

    # Covert 01/01/2020 to SQL date 2020-01-01
    date_purchase = datetime.datetime.strptime(date_string, '%m/%d/%Y').strftime('%Y-%m-%d')
    date_vacation = "No vacation"
    date_week = datetime.datetime.strptime(date_string, '%m/%d/%Y').strftime('%W')
    date_month = datetime.datetime.strptime(date_string, '%m/%d/%Y').strftime('%m')
   
    if "2020-02-15" <= date_purchase and "2020-03-01" >= date_purchase :
        date_vacation = "Voorjaarsvakantie"
        
    print (date_purchase, date_vacation, date_week, date_month)
    
    cursor.execute("""
    SELECT * FROM dim_date
    WHERE date_purchase = '"""+date_purchase+"""'
    AND date_vacation = '"""+date_vacation+""""'
    AND date_week = '"""+date_week+""""'
    OR date_month = '"""+date_month+""""'
 
Share this answer
 
Comments
Richard MacCutchan 8-Jun-22 11:17am    
Is this the solution to your problem? if not then please remove it and use the Improve question link above, and add complete details of what is not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900