1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
import os, sqlite3
db_file = os.path.join(os.path.dirname(__file__), 'test.db')
if os.path.isfile(db_file): os.remove(db_file)
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute('create table user(id varchar(20) primary key, name varchar(20), score int)')
cursor.execute(r"insert into user values ('A-001', 'Adam', 95)")
cursor.execute(r"insert into user values ('A-002', 'Bart', 62)")
cursor.execute(r"insert into user values ('A-003', 'Lisa', 78)")
cursor.close()
conn.commit()
conn.close()
def get_score_in(low, high): conn = sqlite3.connect(db_file) cursor = conn.cursor() cursor.execute(r"select * from user order by score") values = cursor.fetchall() List = [] for result in values: if result[2] >= low and result[2]<=high: List.append(result[1]) cursor.close() conn.close() return List ' 返回指定分数区间的名字,按分数从低到高排序 '
def get_score_in(low, high): conn = sqlite3.connect(db_file) cursor = conn.cursor() cursor.execute(r"select * from user where score between ? and ? order by score",(low,high)) values = cursor.fetchall() name = [x[1] for x in values] cursor.close() conn.close() return name ' 返回指定分数区间的名字,按分数从低到高排序 '
assert get_score_in(80, 95) == ['Adam'], get_score_in(80, 95)
assert get_score_in(60, 80) == ['Bart', 'Lisa'], get_score_in(60, 80)
assert get_score_in(60, 100) == ['Bart', 'Lisa', 'Adam'],get_score_in(60, 100)
print('Pass')
|