利用python将沪深300股票历史数据存储在sqlite3

一、环境准备

1、python3中自带了sqlite3

参考https://www.runoob.com/sqlite/sqlite-tutorial.html

2、在sqlite中建表


CREATE TABLE [stock]
(
[id] NVARCHAR(48),
[name] NVARCHAR(24),	
[code] NVARCHAR(24),
    [date] INTEGER  NOT NULL,
    [open] REAL  ,
    [close] REAL  , 
    [high] REAL  , 
    [low] REAL  , 
    [volume] REAL  ,
    [extend_num] REAL ,
    [extend1] NVARCHAR(30),
    [extend2] NVARCHAR(30),
    [extend3] NVARCHAR(30),
 CONSTRAINT [PK_stock] PRIMARY KEY  ([id])
);
DELETE  from stock ;
CREATE UNIQUE INDEX stock_code_date_I ON stock(code, date);```


二、构造语句

1、查询

```bash
def select():
    db_path = r'D:\Users\Administrator\PycharmProjects\Stocks\tushareTest\stock\data_sqlite\finance.db'
    conn = sqlite3.connect(db_path)
    cur = conn.cursor()
    cur.execute('select * from stock')
    print(cur```
posted @ 2024-04-26 18:31  木头左  阅读(79)  评论(0编辑  收藏  举报