Database column in a while loop isn't getting changed - python

So I am changing a value inside my database on my website. I am sure it is getting changed. I am printing the value
computer.directory_response = "example" # not the actual value it's always changing
db.session.commit()
print(computer.directory_response)
I can see the printed value in the console. meanwhile, I have a program that sends a request to /computers/1/ajax-dir. the problem in the code is when I am doing a while loop to check if the value is example (to be sure I just implemented the if statement inside the while loop)
while(computer.directory_response == ""):
if computer.directory_response != "":
break
else:
pass
at the beginning it's empty. Yes. But even when I change the value in the first part of code I showed you it's still in the while loop. I don't understand why it doesn't change I am using flask as a backend language

It seems that you aren't adding the computer to the db.session.
As that's the case you should add this before the db.commit():
db.session.add(computer)

Related

Continuing function after else

thanks in advance for the help!
I'm building a simple program; the idea is for it to periodically check whether a variable has changed and, if it has, do something and, if not, check again. Use case: show a graph derived from the user's current URL in their browser; if the URL is unchanged, do nothing, if it changes, redraw the graph.
I'm running into an issue; I want my function to keep running while the condition is met, and if the condition is not met do something else, but then keep running.
Here's my while function code; my IDE and reading are telling me that "continue" is not permitted here: is there another way that I can keep my function active? Conversely, please do let me know if this is a foolish way to achieve what I'm trying to do!
while new_value != previous_value:
#wait
#do something
#put contents of new_value into previous_value
#update new_value from external source (e.g. URL from browser, which may or not be have changed)
else:
#wait
#do nothing
#put contents of new_value into previous_value
#update new_value from external source
continue
that's an alright start. The while loop will stop if the values are identical which is often. Inside the while loop you can add an if statement for the desired result. While True keeps going until stopped.
while True:
#wait for a couple of seconds
if new_value != previous_value:
#do something
#put contents of new_value into previous_value
#update new_value from external source
I would try something like this:
while True:
#wait some seconds if you need
if(new_value != previous_value):
#do something
else:
#update new_value from external source
I would use a while loop to keep your program running, and then use an if/else statement within the loop. Then just use break statement to stop running the loop.
while True:
if new_value != previous_value:
# Run your code
else:
# Run your code
# When I want the loop to end
break
The issue here is that the 'else' statement, when used like this, is being executed once your loop is already broken. As such there is nothing to 'continue' and your IDE is flagging a syntax error.
This is a somewhat obscure Python construct, but it (and the reasoning behind it) are well explained here at 15m53s.
What you're probably meaning to do is this:
while True:
if new_value != previous_value:
# Do one thing
else:
# Do a different thing
# You will need to specify an end condition,
# or it will continue looping indefinitely.
if exit_conditon_met:
break

SET working every other time Python MySQL

Im trying to set a value in my table with a given ID. When I run this code I am getting a return code of 1 on 'cursor.execute(updateStr)' the first time I run it, but it executes without issue and returns 0 when I run it a second time. There is no exception raised and I am not sure how to retrieve the actual error message. What could cause this problem and how do I dig deeper into the actual error? Thanks for looking.
updateStr = "UPDATE db.table SET OverrideVal = '{0}' WHERE table.OverrideID ={1};".format(overrideVal, overrideID)
returnCode = cursor.execute(updateStr)
if returnCode == 0:
cursor.execute("COMMIT")
else:
cursor.execute("ROLLBACK")
I don't think the execute command returns anything meaningful. Python's DB-API 2.0 states this about the execute method:
Return values are not defined.
Did you check to see if your updates are actually working? (Be sure to commit them).

while changing directory in os python 2 program stops running in next itiration

I've got a python 2 script which does only work on the first loop. It will stop working on next loop.
Just not working like I´m expectig it - no error messages.
This is the used code:
Although I cannot be sure without actually being able to try it (via copy and paste) it looks like a problem with "before", and where you have placed the raw_input().
If you placed the raw_input() in the loop straight after the while loop you could remove the raw_input outside of your code and only use one.
Second since the value of before never actually clears the else statement always executes. This means you keep adding the filepath held in before to os.chdir() (line 13) returning an error.
At the end of the while loop you should clear before (i.e. before = "")

Why won't my Python function execute?

I'm quite new to coding. I've wrote a simple piece of code which will take a URL as an input, load the URL and put the status code for that URL in a variable. I have put this in a function. But when I run the code I get no errors and yet the function won't run. It seems like the interpreter is just flying past my function. I know I have some simple error but no matter how much I search I can't fix it. Please help me understand. Oh, and the code is incomplete. But the function should run.
import urllib.request, click, threading
url = input("Enter domain name >> \n")
def get_stat():
status = urllib.request.urlopen("http://"+url).getcode()
if status == 200:
return "Site loads normally!\nHost Said: '%s'!" %(str(status))
else:
return "Site Needs To Be Checked!\nHost Said: '%s'!" %(str(status))
get_stat()
if click.confirm("Would you like to set a uptime watch?"):
click.echo("You just confirmed something!!")
count = input("Enter the number of times you wish your site to be checked: ")
interval = input("Enter the time interval for status requests (Time is in minutes): ")
You function certainly is working. The problem you are facing is that you are returning a value from get_stat() (this is what the return statement does) but you never actually say that this value should print.
My guess is you want to print the values that are returned in which case you need to add the print statement:
print(get_stat())
Or you could store what the value as a variable:
a = get_stat()
print(a)
As said in quamrana's comment below although the following method is considered bad practice you can put the print() statement inside your function for debugging purposes and replace it later:
if status == 200:
print("Site loads normally!\nHost Said: '%s'!" %(str(status)))
else:
print("Site Needs To Be Checked!\nHost Said: '%s'!" %(str(status)))
This will prove that the function is indeed being executed.
This post might help you understand a little bit better what the return statement does and how you can get values from it.

How to prevent a repeat of a program call within a while loop

I am trying to prevent this switch function from constantly repeating within the probability while loop, i want it to be called once promoting an input and then using the return of that input for each time in the while loop instead of asking every time
Click here to see screenshot of code
(it won't let me add a second picture of the switch function so ill just copy and paste it)
def switch_door():
switch=raw_input("Switch doors?:")
if switch!="y" and switch!="n":
return "Incorrect inputs"
elif switch=='y':
return True
elif switch=='n':
return False
You may set a variable e.g.if_switch=switch_door() in the probability() function before your while loop and pass that variable to your simulation function as a parameter.
Note that you will need to change your simulation definition to e.g. def simulation(doors, if_switch):; you will also need to change these two lines:
if switch_door()==True: to if if_switch==True:
elif switch_door()==False: to simply else:
Now your problem should solved.

Categories