I have created a db file from data downloaded from online. I used the following code to create the database:
kiwoom.ohlcv = {'date': [], 'open': [], 'high': [], 'low': [], 'close': [], 'volume': []}
df = pd.DataFrame(kiwoom.ohlcv, columns=['open', 'high', 'low', 'close', 'volume'], index=kiwoom.ohlcv['date'])
con = sqlite3.connect("c:/Users/Runner/Data/Stock/Dayprice_kosdaq_filtered.db")
df.to_sql(stock_code, con, if_exists='replace')
Here, the index data of the dataframe should have become an another column in the database according to the pandas.DataFrame documentation:
index:bool, default True
Write DataFrame index as a column. Uses index_label as the column name in the table.
The problem however is that if I try to call the data under index column using SELECT:
con1 = sqlite3.connect("c://Users//Runner//Data//Stock//Dayprice_kosdaq_filtered.db")
cursor = con1.cursor()
cursor.execute("SELECT 'index' FROM '101360' WHERE close = 24850")
table_list = cursor.fetchall()
print(table_list)
I get the following result, where '101360' is the table name and close is a column:
runfile('C:/Users/Runner/Spyder Projects/Stocks/Get Data/untitled1.py', wdir='C:/Users/Runner/Spyder Projects/Stocks/Get Data') [('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',), ('index',)]
When I check the values for index column through DB Browser, I do see TEXT values of dates but when trying to access it via python code, I get 'index' values. What has gone wrong and how could I fix this? Thanks
You can try covering the column (index, close) in the query with [] since both columns are keywords as below:
con1 = sqlite3.connect("c://Users//Runner//Data//Stock//Dayprice_kosdaq_filtered.db")
cursor = con1.cursor()
query = cursor.execute("SELECT [index] FROM '101360' WHERE [close] = 24850")
client_df = pd.DataFrame(query)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.