期货日数据维护与使用_日数据维护_sqlite3数据库创建

2024-01-08 17:53:57

目录

写在前面:

初始准备

开始编写数据库代码

t_product?

t_symbol_basemsg

t_main_symbol

t_online_symbol

t_last30_daily

小贴士


写在前面:

本文默认已经创建了项目,如果不知道如何创建一个空项目的,请参看以下两篇博文

PyQt5将项目搬到一个新的虚拟环境中
https://blog.csdn.net/m0_37967652/article/details/122625280
python_PyQt5开发工具结构基础
https://blog.csdn.net/m0_37967652/article/details/131969032

初始准备

python自带有sqlite3的包,如果没有的话,自己安装下

pip install sqlite3 -i https://pypi.tuna.tsinghua.edu.cn/simple

创建一个py文件,命名为 sqlite_tool.py(文件名大家自行定义,只要后面使用能找到就行)

将数据库路径定义为一个常量

YOUKUANG_DB_NAME = 【项目路径】+‘youkuang_db.db’

开始编写数据库代码

t_product?

def create_product_table():
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    c.execute('''
    create table if not exists t_product 
    (code text, name text,exchange_name text)
    ''')
    conn.commit()
    conn.close()
    pass

def batch_insert_product(pre_list: List):
    '''
    :param pre_list: [(code,name),(code,name)]
    :return:
    '''
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
    insert into t_product values (?,?)
    '''
    c.executemany(sql_str,pre_list)
    # e
    conn.commit()
    conn.close()
    pass

def one_insert_product(pre_one:List):
    '''
    :param pre_one: [code,name]
    :return:
    '''
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
    insert into t_product values (?,?)
    '''
    c.execute(sql_str,pre_one)
    # e
    conn.commit()
    conn.close()
    pass

def query_products_of_exchange_name(exchange_name:str):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    exchange_name = '\'' + exchange_name + '\''
    sql_str = '''
            select code,name from t_product where exchange_name={exchange_name}
            '''.format(exchange_name=exchange_name)
    c.execute(sql_str)
    res_list = c.fetchall()
    # e
    conn.commit()
    conn.close()
    return res_list

def query_all_product_codes_of_product():
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
                select code from t_product
                '''
    c.execute(sql_str)
    res_list = c.fetchall()
    # e
    conn.commit()
    conn.close()
    res_list00 = []
    for item in res_list:
        res_list00.append(item[0])
    return res_list00

create_product_table? 创建表

batch_insert_product 批量插入数据

one_insert_product 单条数据插入

query_products_of_exchange_name 查询某个交易所下的品种信息

query_all_product_codes_of_product 查询所有品种代码

t_symbol_basemsg

def create_symbol_basemsg_table():
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    c.execute('''
        create table if not exists t_symbol_basemsg
            (
            ticker text, 
            listDate text,
            product_code text,
            minChgPriceNum float,
            minChgPriceUnit text,
            limitUpNum float,
            limitUpUnit text,
            limitDownNum float,
            limitDownUnit text,
            contMultNum float,
            contMultUnit text,
            tradeMarginRatio float,
            deliYear integer,
            deliMonth integer,
            lastTradeDate text,
            firstDeliDate text,
            lastDeliDate text,
            tradeCommiNum float,
            tradeCommiUnit text
            )
        ''')
    conn.commit()
    conn.close()
    pass

def batch_insert_symbol_basemsg(pre_list:List):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
        insert into t_symbol_basemsg values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
        '''
    c.executemany(sql_str, pre_list)
    # e
    conn.commit()
    conn.close()
    pass

create_symbol_basemsg_table 创建表

batch_insert_symbol_basemsg 批量插入数据

t_main_symbol

def create_main_symbol_table():
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    c.execute('''
    create table if not exists t_main_symbol 
    (product_code text, ticker text,deliYear integer,start_date text)
    ''')
    conn.commit()
    conn.close()
    pass

def one_insert_main_symbol(pre_one:List):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
            insert into t_main_symbol values (?,?,?,?)
            '''
    c.execute(sql_str, pre_one)
    # e
    conn.commit()
    conn.close()
    pass

def update_main_symbol(product_code:str,ticker:str,deliYear:int,start_date_str:str):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    ticker = '\''+ticker+'\''
    start_date_str = '\'' + start_date_str + '\''
    product_code = '\'' + product_code + '\''
    sql_str = '''
    update t_main_symbol set ticker={ticker},deliYear={deliYear},start_date={start_date} where product_code={product_code}
    '''.format(ticker=ticker,deliYear=deliYear,start_date=start_date_str,product_code=product_code)
    c.execute(sql_str)
    # e
    conn.commit()
    conn.close()
    pass

create_main_symbol_table 创建表

one_insert_main_symbol 单条数据插入

update_main_symbol 更新数据

t_online_symbol

def create_online_symbol_table():
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    c.execute('''
    create table if not exists t_online_symbol 
    (product_code text, ticker text, deliYear integer, newest_date text)
    ''')
    conn.commit()
    conn.close()
    pass

def one_insert_online_symbol(pre_one:List):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
            insert into t_online_symbol values (?,?,?,?)
            '''
    c.execute(sql_str, pre_one)
    # e
    conn.commit()
    conn.close()
    pass

def batch_insert_online_symbol(pre_list:List):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
                insert into t_online_symbol values (?,?,?,?)
                '''
    c.executemany(sql_str, pre_list)
    # e
    conn.commit()
    conn.close()
    pass

create_online_symbol_table 创建表

one_insert_online_symbol 单条数据插入

batch_insert_online_symbol 批量数据插入

t_last30_daily

def create_last30_daily_table():
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    c.execute('''
        create table if not exists t_last30_daily 
        (
        product_code text,
        ticker text,
        deliYear integer,
        tradeDate text,
        openPrice float,
        highestPrice float,
        lowestPrice float,
        closePrice float,
        settlePrice float,
        turnoverVol integer,
        turnoverValue integer,
        openInt integer
        )
        ''')
    conn.commit()
    conn.close()
    pass

def batch_insert_last30_daily(pre_list: List):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    sql_str = '''
    insert into t_last30_daily values (?,?,?,?,?,?,?,?,?,?,?,?)
    '''
    c.executemany(sql_str,pre_list)
    # e
    conn.commit()
    conn.close()
    pass

def query_daily_by_pro_in_last30_daily(product_code:str):
    conn = sqlite3.connect(YOUKUANG_DB_NAME)
    c = conn.cursor()
    # s
    product_code_str = '\'' + product_code + '\''
    sql_str = '''
                select product_code,ticker,deliYear,tradeDate,openPrice,highestPrice,lowestPrice,closePrice,settlePrice,turnoverVol,turnoverValue,openInt from t_last30_daily where product_code={product_code}
                '''.format(product_code=product_code_str)
    c.execute(sql_str)
    res_list = c.fetchall()
    # e
    conn.commit()
    conn.close()
    return res_list

create_last30_daily_table 创建表

batch_insert_last30_daily 批量插入数据

query_daily_by_pro_in_last30_daily 查询某品种的最近日数据

小贴士

sqlite3查看工具,SQLiteStudio,网上可以免费下载安装

文章来源:https://blog.csdn.net/m0_37967652/article/details/135408267
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。