2020-07-25数据库应用------问答测试

        注意:从python2.5版本以上就内置了SQLite3,所以可以直接使用

1.访问数据库的步骤

1.导入Python SQLite数据库模块

import sqlite3

2.建立数据库连接,返回Connection对象

con = sqlite3.connect(connectstring)

3.创建游标对象

cur = con.cursor()

4.使用Cursor对象的execute()方法执行SQL命令返回结果集

cur.execute("create table category(id primary key,sort,name)")

cur.excute("insert into category values(1,1,'computer')")

5.获取游标的查询结果集

cur.execute("select * from catagory")

print(cur.fetchall())

6.数据库的提交和回滚

提交:con.commit()

回滚:con.rollback()

7.关闭Cursor对象和Connection对象

cur.close()

con.close()

2.程序设计步骤

2.1生成试题库

'''

import sqlite3

conn = sqlite3.connect('test.db')

cursor = conn.cursor()

cursor.execute('create table if not exists [exam]([question] varchar(80) null, [Answer_A] varchar(1) null,[Answer_B] varchar(1) null,[Answer_C] varchar(1) null,[Answer_D] varchar(1) null,[right_Answer] varchar(1) null)')

cursor.execute("insert into exam (question,Answer_A,Answer_B,Answer_C,Answer_D,right_Answer) values('夜郎自大中“夜郎”指的是现在哪个地方?','贵州','云南','广西','福建','A')")

cursor.execute("insert into exam (question,Answer_A,Answer_B,Answer_C,Answer_D,right_Answer) values('在中国历史上是谁发明了麻药?','孙思邈','华佗','张仲景','扁鹊','B')")

cursor.execute("insert into exam (question,Answer_A,Answer_B,Answer_C,Answer_D,right_Answer) values('京剧中的花旦指?','年轻男子','年轻女子','年长男子','年长女子','B')")

cursor.execute("insert into exam (question,Answer_A,Answer_B,Answer_C,Answer_D,right_Answer) values('篮球比赛每队几个人?','4','5','6','7','B')")

cursor.execute("insert into exam (question,Answer_A,Answer_B,Answer_C,Answer_D,right_Answer) values('在天愿作比翼鸟,在地愿为连理枝。讲述的是谁的爱情故事?','焦仲卿和刘兰芝','梁山伯与祝英台','崔莺莺和张生','杨贵妃和唐明皇','D')")

print(cursor.rowcount)

cursor.close()

conn.commit()

conn.close()

'''

2.2读取试题信息

'''

conn = sqlite3.connect('test.db')

cursor = conn.cursor()

cursor.execute('select * from exam')

values = cursor.fetchall()

cursor.close()

conn.close()

'''

2.3界面和逻辑设计

'''

import tkinter

from tkinter import *

from tkinter.messagebox import *

def callNext():

    global k

    global score

    useranswer = r.get()

    print(r.get())

    if useranswer == values[k][5]:

        showinfo("恭喜!","你答对了!")

        score += 20

    else:

        showinfo("遗憾!","你错了哦!")

    k = k + 1

    if k >= len(values):

        showinfo("提示","题目做完了")

        return

    timu["text"] = values[k][0]

    radio1["text"] = values[k][1]

    radio2["text"] = values[k][2]   

    radio3["text"] = values[k][3]   

    radio4["text"] = values[k][4]

    r.set('E')


def callResult():

    showinfo("你的得分",str(score))

'''

界面布局代码:

'''

root = tkinter.Tk()

root.title('Python智力问答游戏')

root.geometry("500x200")

r = tkinter.StringVar()

r.set('E')

k = 0

score = 0

timu = tkinter.Label(root,text = values[k][0])

timu.pack()

f1 = Frame(root)

f1.pack()

radio1 = tkinter.Radiobutton(f1,variable = r,value = 'A',text = values[k][1])

radio1.pack()

radio2 = tkinter.Radiobutton(f1,variable = r,value = 'B',text = values[k][2])

radio2.pack()

radio3 = tkinter.Radiobutton(f1,variable = r,value = 'C',text = values[k][3])

radio3.pack()

radio4 = tkinter.Radiobutton(f1,variable = r,value = 'D',text = values[k][4])

radio4.pack()

f2 = Frame(root)

f2.pack()

Button(f2,text = '下一题',command = callNext).pack(side = LEFT)

Button(f2,text = '结  果',command = callResult).pack(side = LEFT)

root.mainloop()

'''

运行结果如下:



你可能感兴趣的:(2020-07-25数据库应用------问答测试)