My aim is to get the last price from a histoy table (History_price) and inject it in a parameter table (param_forex).
I have defined the following function:
mycursor.execute(f"UPDATE `param_forex` SET `Rate` = %s, `Update_Time` = CURRENT_TIME() WHERE `param_forex`.`Ticker` LIKE %s",
(Rate, Ticker))
mydb.commit()
mycursor.close
The function works on its own. It simply fills a table with new prices on some particular lines.
Now I try to incorporate it in my loop. It’s actually a double loop because of how mycursor works in mysql.connector
def fillpricetable_assetid() :
for x in ['EURUSD','EURJPY','EURGBP']:
mycursor.execute(f"SELECT `Price` FROM `History_price` WHERE `Ticker` LIKE '{x}' ORDER BY `History_price`.`Time` DESC LIMIT 1")
for y in mycursor:
updateForexdb(x.lower(),y[0])
And it does not work anymore…
I get
File "c:...library_import.py", line 116, in <module>
fillpricetable_assetid()
File "c:...library_import.py", line 89, in fillpricetable_assetid
updateForexdb(x.lower(),y[0])
NameError: name 'updateForexdb' is not defined
How is that possible, when I have just defined it above??
2
Answers
I have interverted the order and now it works fine.
Instead of running multiple queries in a loop, why not just join the tables?
MySQL (any version):
MySQL (>= 8.0):
Or, even better, don’t store the redundant xrate values in
param_forex
and just access directly fromHistory_price
. Make sure you have an index on(Ticker, Time)
.