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

目录

写在前面:

初始准备

开始编写数据库代码

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,网上可以免费下载安装

相关推荐
不爱学习的啊Biao1 分钟前
初识mysql数据库
数据库·mysql·oracle
宛唐羁客1 小时前
ODBC连接PostgreSQL数据库后,网卡DOWN后,客户端进程阻塞问题解决方法
网络·数据库
吴半杯1 小时前
Redis-monitor安装与配置
数据库·redis·缓存
mariokkm2 小时前
Django一分钟:django中收集关联对象关联数据的方法
android·django·sqlite
是桃萌萌鸭~2 小时前
mysqldbcompare 使用及参数详解
数据库·mysql
gma9992 小时前
brpc 与 Etcd 二次封装
数据库·c++·rpc·etcd
Yz98762 小时前
hive复杂数据类型Array & Map & Struct & 炸裂函数explode
大数据·数据库·数据仓库·hive·hadoop·数据库开发·big data
小鹿撞出了脑震荡3 小时前
SQLite3语句以及用实现FMDB数据存储的学习
数据库·学习·sqlite
小草儿7993 小时前
gbase8s之mysql的show命令实现
数据库·mysql
斯普信专业组3 小时前
MongoDB调优利器:掌握性能分析工具mongostat
数据库·mongodb·mongostat