i am a newbees for programming, i have an db file with some date, open, high, low , close data in it, and name with 0001.HK; 0002.HK; 0003.HK then i try to build a loop to take out some data in the database.
conn = sqlite3.connect(os.path.join('data', "hkprice.db"))
def read_price(stock_id):
connect = 'select Date, Open, High, Low, Close, Volume from ' + stock_id
df = pd.read_sql(connect, conn,index_col=['Date'], parse_dates=['Date'])
for y in range(1 ,2):
read_price(str(y).zfill(4) + '.HK')
when it output it show: Execution failed on sql 'select Date, Open, High, Low, Close, Volume from 0001.HK': unrecognized token: "0001.HK"
but i should have the 0001.HK table in the database what should i do?
'around the value you pass,read_price("'" + str(y).zfill(4) + ".HK'")try-exceptstatement specifying error type and let it simply pass in case table doesn't exist.except: pass(its didn't work), but i find out that i shouldn't loop the file name with number range... in my sql db, some of the number are not exist, such as 0012.HK(exist); 0013.HK(not exist); 0014.HK(exist), so i think i shouldn't loop with the file name, but i am not sure about how to read the sql one by oneexcept: passshould work, although it's a very bad habit to use it like this. Instead doexcept %ExceptionClass%:where you replace %ExceptionClass% with the actual class of exception you are getting. Make surepassis on the next line and properly indented.