Python 修改json文件以及操作sqlite3数据库

import json

# data = dict(
#     total_pulps=0,
#     total_carts=0,
#     average_pulp=0,
#     last_record=0,
#     day_count=0,
#     night_count=0,
#     total_day_count=0,
#     total_night_count=0
# )
#
# with open('work_record.json', 'w') as f:
#     json.dump(data, f)

# 修改json
with open('work_record.json', 'w+') as f:
    data = json.load(f)
data['total_pulps'] += 28
data['total_carts'] += 1
data['average_pulp'] = int(data['total_pulps'] / data['total_carts'])
data['last_record'] = 28
data['day_count'] += 28
data['total_day_count'] += 28
data['test'] = 'test'
with open('work_record.json', 'a') as f:
    json.dump(data, f)
import sqlite3

from config import PATH

class Databse:

    def __init__(self):
        self.conn = sqlite3.connect(PATH['database'])
        self.cs = self.conn.cursor()

    def createTable(self):
        sql = '''CREATE TABLE jobs( id      INTEGER     PRIMARY KEY AUTOINCREMENT UNIQUE,
                                    jobid   INT         NOT NULL,
                                    time    TEXT        NOT NULL,
                                    count   INT         NOT NULL,
                                    path    TEXT        NOT NULL)'''
        self.cs.execute(sql)
        self.conn.commit()

    def insertTable(self, time, count, path, jobid=None):
        if jobid is not None:
            sql = f'''INSERT INTO jobs (jobid, time, count, path)
                                VALUES ({jobid}, '{time}', {count}, '{path}')'''
            print(sql)
            self.cs.execute(sql)
            self.conn.commit()

    def deleteTable(self, id=None):
        if id is not None:
            sql = f'''DELETE FROM jobs WHERE id = {id}'''
            self.cs.execute(sql)
            self.conn.commit()

    def modifyTable(self, column, value, id=None):
        if id is not None:
            sql = f'''UPDATE jobs set {column} = {value} WHERE id = {id}'''
            self.cs.execute(sql)
            self.conn.commit()

    def searchTable(self, number, sql='select * from jobs'):
        self.cs.execute(sql)
        return self.cs.fetchmany(number)

    def  __del__(self):
        self.cs.close()
        self.conn.close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值