sqlite基本操作

# -*- coding: utf-8 -*-
# @Time    : 2023/7/21 15:22
# @Author  : Cocktail_py

import os
import sys
import sqlite3
APP_ROOT = getattr(sys, '__APP_ROOT__', os.path.split(os.path.realpath(__file__))[0])

APP_PATH = getattr(sys, '__APP_PATH__', os.path.join(APP_ROOT, 'packages'))

APP_PATH and sys.path.insert(0, APP_PATH)

db_path = APP_ROOT + '/' + "data"
if not os.path.exists(db_path):
    os.makedirs(db_path)


def dict_factory(cursor,row):
    """以字典形式返回值"""
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

conn = sqlite3.connect(db_path+"/"+"eth_gas_price.db")
# 以字典形式返回查询数据
conn.row_factory = dict_factory
cursor = conn.cursor()

# 不存在创建相应数据库
db_sql = """CREATE TABLE IF NOT EXISTS gas_price (
                id integer primary key autoincrement,
                coin_name varchar(15) not null UNIQUE,
                low integer,
                avg integer,
                high integer,
                c_time integer
            );"""

cursor.execute(db_sql)

cursor.execute("select * from gas_price limit 10;")
# cursor.fetchall()

conn.commit()


cursor.close()
conn.close()


# 参考:https://blog.youkuaiyun.com/qq_39147299/article/details/121953056

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cocktail_py

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值