将Sqlite3数据库挂在内存上处理

创作灵感:最近把小学生的口算题从2位数改到3位数,100以内四则运算练习(千纬数学)再次更新,选取难题-优快云博客要不断刷题目,以前100以内的加减乘除也是这样刷出来的,代码如下:

import sqlite3
import random
from time import time
from pathlib import Path

#导入必要的库

resources_folder = Path(__file__).joinpath("../resources/").resolve()
db_filepath = resources_folder.joinpath("qwsx.db")
#db_filepath = '/storage/emulated/0/Pictures/qwsx.db'
#上面是数据库的存放位置,生成手机app请参考我以前的文章
#oppo版 需要用电脑调试应删除下面5行

def gettid(s1,fh,s2,dan):
    conn = sqlite3.connect(db_filepath, timeout=10, check_same_thread=False)
    c = conn.cursor()
    #如果公式存在,提取tid
    cursor = c.execute("SELECT count(tid) from ys where s1 = ? and fh = ? and s2 = ?;", (s1,fh,s2,))
    row = cursor.fetchone()
    ctid = row[0]
    #如果公式不存在,插入公式到数据库
    if ctid == 0:
        c.execute("INSERT INTO ys(s1,fh,s2,dan,cs,cuo) VALUES (?,?,?,?,0,0);", (s1,fh,s2,dan,))
        conn.commit()
    cursor = c.execute("SELECT tid from ys where s1 = ? and fh = ? and s2 = ? order by tid desc;", (s1,fh,s2,))
    row = cursor.fetchone()
    tid = row[0] 
    c.close()
    conn.close()
    return(tid)
#获取tid,题目的id
def settm(nd):    
    if nd ==1:
        jj = random.randint(0,1)
    elif nd ==2:
        jj = random.randint(0,3)
    if jj == 0:
        #为加法 
        s1 = random.randint(0,100)
        s2 = random.randint(0,(100 - s1))
        cvalue = str(s1) + "+" + str(s2) + "=" 
        dan = s1 + s2
        hd = 1
        tid = gettid(s1,jj,s2,dan)
        ii = random.randint(0,4) #0为提交答案
        if ii == 2:
            cvalue = "□+" + str(s2) + "=" + str(dan) + ",□为"
            dan = s1
        elif ii == 3:
            cvalue = str(s1) + "+□=" + str(dan) + ",□为"
            dan = s2
        elif ii ==4 and s2 > 0:#a+0=a,a-0=a,可以是+-
            cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
            dan = jj
            hd = 4 #hd4为符号
    elif jj ==1:
        s1 = random.randint(0,100)
        s2 = random.randint(0,s1)
        cvalue = str(s1) + "-" + str(s2) + "=" 
        dan = s1 - s2
        hd = 1
        tid = gettid(s1,jj,s2,dan)
        ii = random.randint(0,4) #0为提交答案
        if ii == 2:
            cvalue = "□-" + str(s2) + "=" + str(dan) + ",□为"
            dan = s1
        elif ii == 3:
            cvalue = str(s1) + "-□=" + str(dan) + ",□为"
            dan = s2
        elif ii ==4 and s2 > 0:#a+0=a,a-0=a,可以是+-
            cvalue = str(s1) + "□" + str(s2) + "=" + str(dan) + ",□为"
            dan = jj
            hd = 4 #hd4为符号
    elif jj ==2:
        #乘法
        s1 = random.randint(1,10)
        s2 = random.randint(0,int(100 / s1))
        cvalue = str(s1) + "×" + str(s2) + "="
        dan = s1 * s2
        hd = 1
        tid = gettid(s1,jj,s2,dan)
        ii = random.randint(0,4) #0为提交答案
        if ii == 2:
            cvalue = "□×" + str(s2) + "=" + str(dan) + ",□为"
            da
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值