- sqlite3数据库属于python自带的数据库,属于小型数据库,语句简洁、可视化、编辑简单、无代码改变表内结构。
- import sqlite3即可。


在pychrm中配置sqlite3环境时下载驱动失败怎么办? - 乐之之 - 博客园 (cnblogs.com)
| select * from sqlite_master |
| |
| import sqlite3 |
| conn = sqlite3.connect('sqlite.db') |
| sql1 = '''create table user_tb( _id integer primary key autoincrement, name text, pass text, gender text)''' myCursor.execute(sql1) |
| |
| sql2 = '''create table order_tb( _id integer primary key autoincrement, item_name text, item_price real, item_number real, user_id inteter, foreign key(user_id) references user_tb)''' myCursor.execute(sql2) |
- 变量名 = """insert into 表名 values (null,'数据1','数据2','数据3')"""
| sql = """insert into user_tb values (null,'九歌','五期','男')""" |
| import sqlite3 |
| |
| |
| conn = sqlite3.connect('sqlite.db') |
| |
| |
| mycursor = conn.cursor() |
| |
| |
| sql = """insert into user_tb values (null,'九歌','五期','男')""" |
| mycursor.execute(sql) |
| |
| conn.commit() |
| |
| mycursor.close() |
| |
| conn.close() |
| sql = insert into 表名 values |
| executmany(sql,data) |
| |
| import sqlite3 |
| |
| |
| |
| conn = sqlite3.connect('sqlite.db') |
| |
| |
| myCursor = conn.cursor() |
| |
| |
| |
| list_student = [('张三','一班','男'), |
| ('王五','一班','男'), |
| ('李四','一班','男'), |
| ('赵六','一班','男')] |
| |
| sql_entry = 'insert into user_tb values (null,?,?,?)' |
| |
| myCursor.executemany(sql_entry,list_student) |
| |
| |
| conn.commit() |
| |
| |
| myCursor.close() |
| |
| |
| conn.close() |
- update 数据表 set 字段名 = 值 where 条件表达式
| import sqlite3 |
| |
| |
| conn = sqlite3.connect('sqlite.db') |
| |
| |
| mycursor = conn.cursor() |
| |
| |
| mycursor.executemany('update user_tb set name=? where _id=?', |
| ( |
| ('张三',1), |
| ('李四',2) |
| )) |
| |
| print('修改的记录条数:',mycursor.rowcount) |
| |
| conn.commit() |
| |
| mycursor.close() |
| |
| conn.close() |
| select * from user_tb where 字段名 = 值 |
| select * from user_tb where gender = '男' |
- 以上语句翻译:从user_tb表中将所有关于gender为’男‘的数据查找出来。
- 除了用上面的方法,在PyCharm中,需要编写sql语句来查询数据表的记录。 在此有三个方法:fetchone(),fetchmany(),fetchall()。其中fetch的英文意思就是“抓取”,那么下面就好理解了。
- fetchone()——查找一条记录
- fetchmany()——查找多条记录(如fetchmany(3),就是查询3条记录)
- fetchall()——查找所有记录
| |
| import sqlite3 |
| |
| |
| |
| conn = sqlite3.connect('sqlite.db') |
| |
| |
| myCursor = conn.cursor() |
| |
| |
| |
| myCursor.execute('select * from user_tb where _id > ?', (2,)) |
| |
| |
| |
| for col in (myCursor.description): |
| print(col[0], end='\t') |
| print('\n'+'-'*26) |
| |
| |
| while True: |
| |
| row = myCursor.fetchone() |
| |
| if not row: |
| break |
| print(row) |
| |
| |
| |
| |
| myCursor.close() |
| |
| |
| conn.close() |
- delete from 数据表;所有的记录全部删除。
- delete from 数据表 where 条件表达式;
- 定义建表函数。
- 找到对应的表,查询所有的内容,进行遍历添加到新的列表内。
- 若不存在表则返回False,在程序入口创建表。存在则返回True。
| def check(db_name, table_name): |
| |
| |
| conn = sqlite3.connect(db_name) |
| cu = conn.cursor() |
| sql = """select tbl_name from sqlite_master where type='table'""" |
| cu.execute(sql) |
| |
| values = cu.fetchall() |
| print(values) |
| |
| tables = [] |
| for v in values: |
| tables.append(v[0]) |
| if table_name not in tables: |
| return False |
| else: |
| return True |
| |
| def cd(): |
| os.system('cls') |
| print("学员管理系统") |
| print("1、添加学生分数信息") |
| print("2、查看全部学生的分数") |
| print("3、查看数据段内的学生分数") |
| print("4、退出") |
| if __name__ == '__main__': |
| |
| conn = sqlite3.connect("student_score.db") |
| |
| cu = conn.cursor() |
| |
| if (check("student_score.db","scores") == False): |
| sql_1 = """ |
| create table scores(姓名 TEXT, |
| 班级 TEXT, |
| 性别 TEXT, |
| 语文 NUMBER, |
| 数学 NUMBER , |
| 英语 NUMBER, |
| 总分 NUMBER); |
| """ |
| cu.execute(sql_1) |
| while True: |
| op = int(input('请输入需要选择的功能:')) |
| if op == 1: |
| S_name = input("请输入添加到的学生的姓名(如:张三):") |
| S_class = input("请输入要添加的学生班级(如:一班):") |
| S_sex = input("请输入添加的学生的性别:") |
| S_chinese = int(input("请输入该学生的语文成绩(只输入一个数字,如82):")) |
| S_maths = int(input("请输入该学生的数学成绩(只输入一个数字,如82):")) |
| S_English = int(input("请输入该学生的英语成绩(请输入一个数字,如82):")) |
| S_gi = S_chinese + S_maths + S_English |
| data = [(S_name,S_class,S_sex,S_chinese,S_maths,S_English,S_gi)] |
| cu.executemany("insert into scores values (?,?,?,?,?,?,?)",data) |
| |
| conn.commit() |
| print("成功!") |
| os.system("pause") |
| os.system("cls") |
| cd() |
| |
| elif op == 2: |
| info_list = find_tb() |
| print("全部学生的信息(排名部分先后):") |
| for i in range(len(info_list)): |
| print("第"+str(i+1)+"个") |
| print("学生成绩:"+str(info_list[i][0])) |
| print("学生班级:"+str(info_list[i][1])) |
| print("学生性别:"+str(info_list[i][2])) |
| print("学生性别:"+str(info_list[i][2])) |
| print("学生语文成绩:"+str(info_list[i][3])) |
| print("学生数学成绩:"+str(info_list[i][4])) |
| print("学生英语成绩:"+str(info_list[i][5])) |
| print("学生总成绩:"+str(info_list[i][6])) |
| os.system("pause") |
| os.system("cls") |
| cd() |
| elif op == 3: |
| info_list = find_tb() |
| fen = int(input("请输入要查询的分段:")) |
| for i in range(len(info_list)): |
| if info_list[i][6] >=fen: |
| print("查询结果;") |
| print("第"+str(i+1)+"个") |
| print("学生总成绩:"+ str(info_list[i][6])) |
| os.system("pause") |
| os.system("cls") |
| cd() |
| elif op == 4: |
| os.system("cls") |
| break |


【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库