Python Forum
Selection based of variables issue
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selection based of variables issue
#11
What you posted is neither full nor minimal. It's not full because there are no imports, and it's not minimal because your question really doesn't seem to be about databases or web server, so it should be framed without those to keep it as simple as possible. You also didn't describe your input and output clearly.

The code you post should look something like
if '=' not in name + month + year:
    print(1)
elif '=' not in month + year:
    print(2)
elif '=' not in name + year:
    print(3)
elif '=' not in year:
    print(4)
except that you should hard-code values for those variables that reproduce the problem, and explicitly say which number you expect for which variable values. Make sure to run the code you provide here to ensure that it reproduces the problem.

I'm pretty certain that your problem can be very, very simplified, and one it is it'll get an answer quickly.
Reply
#12
I typed it like this because iam using flask and the code was within a view function, hence i couldnt use print.

		if '=' not in name + month + year:
			printdata = "one"
		elif '=' not in month + year:
			printdata = "two"
		elif '=' not in name + year:
			printdata = "three"
		elif '=' not in year:
			printdata = "four"
......
......
return printdata
The returning corresponding values returned are: name, month, and years are form drop down menu values which i seeect by hand frm another view fucntion and if i try to print them they print correctly.

"one"
"two"
"one"
"two"

Numbers three and four never being returned.

Also i checked the values name, month, year that came as
name = request.args.get('name')
month = request.args.get('month')
year = request.args.get('year')

and all being correct.

The code must be in the short circuit logic?
Reply
#13
In case you want to see the whole view function please take a look at:

# =================================================================================================================
# search & display requested info based on name/month/year criteria
# =================================================================================================================
@app.route( '/seek', methods=['GET', 'POST'] )
def seek():

	pdata = ''

	name = request.args.get('name')
	month = request.args.get('month')
	year = request.args.get('year')

	try:
		if '=' not in name + month + year:
			cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) )

		elif '=' not in name + year:
			cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) )

		elif '=' not in month + year:
			cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) )

		elif '=' not in year:
			cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year )
			
		else:
			pdata = pdata + "<h2><font color=red>Πώς να γίνει αναζήτηση αφού δεν επέλεξες ούτε πελάτη ούτε μήνα ή τουλάχιστον το έτος?"


		data = cur.fetchall()

		hits = money = 0

		for row in data:
			hits += 1
			money = money + row[2]


		pdata = pdata + '''
		<body background="/static/img/pelatologio.jpg">
		<center><h2><font color=yellow size=5> Απολαβές από </font> <font color=lime size=5> [ %s - %s - %s ] </font>
		<br><br>
		<font color=white>
		Επισκευές:  <font color=violet> %s </font>
		Σύνολο:     <font color=orangered> %s € </font>
		<br><br>
		<table border=5 cellpadding=5 bgcolor=black>
		<th><u><font color=lime size=5> Πελάτης </th>   <th><u><font color=tomato size=5> Περιγραφή </th>
		<th><u><font color=cyan size=5> Αμοιβή </th>    <th><u><font color=orange size=5> Ημερομηνία </th>
		''' % (name, month, year, hits, money)

		for row in data:
			(clientID, task, price, lastvisit) = row

			cur.execute( '''SELECT name FROM clients WHERE id = %s''', clientID )
			data = cur.fetchone()

			name = data[0]
			lastvisit = lastvisit.strftime('%A, %e %b %Y')

			pdata = pdata + '''
			<tr>
				<td><center><font color=lime   size=5> %s </td>
				<td><center><font color=tomato size=5> %s </td>
				<td><center><font color=cyan   size=5> %s </td>
				<td><center><font color=orange size=5> %s </td>
			</tr>
			''' % (name, task, price, lastvisit)
	except pymysql.ProgrammingError as e:
		print( repr(e) )

	pdata = pdata + "<meta http-equiv='REFRESH' content='200;%s'>" % url_for( 'index' )
	return pdata
Reply
#14
When i go to phpmyadmin and give

SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = "Άκης Τσιάμης") and YEAR(lastvisit) = 2017 ORDER BY lastvisit DESC
i get 2 results as expected.

When in script this runs
        elif '=' not in name + year:
            cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) )
i get NO results at all. That is what i dont understand, why its behaving differently.
Reply
#15
Any help please?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  generate random variables based on a non-standard t-distribution nathalie 4 3,377 Dec-03-2019, 12:11 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020