python-数据库
前导:
mac开启mysql方法:
1)系统偏好设置,左下角的mysql,点击查看是否start状态。
2)终端输入:mysql -u root -p然后回车,输入密码Lswtest@123就进入mysql。
localhost:~ 姓名$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>
一、SQLite3模块
python使用内置SQLite3模块,支持SQLite3数据库的访问和相关的数据库操作。
python3操作SQLite3数据库的基本流程如下:
1)导入相关库或模块(SQLite3)
2)使用connect()连接数据库并获取数据库连接对象,提供如下方法:
.cursor()方法来创建一个游标对象
.commit()方法来处理事务提交
.rollback()方法来处理事务回滚
.close()方法来关闭一个数据库连接
3)使用con.cursor()获取游标对象
4)使用游标对象的方法(execute()、executemany()、fetchall()等)来操作数据库,实现插入/修改/删除操作,并查询获取显示相关的记录。
连接函数sqlite3.connect()有如下两个常用参数:
database:表示要访问的数据库名。
timeout:表示访问数据的超时设定。
5)使用close()关闭游标对象和数据库连接,数据库操作完成后,必须调用close关闭,减轻数据库服务器的压力。
例1:使用SQLite3创建表
#导入sqlite3模块 import sqlite3 #硬盘上创建连接 con=sqlite3.connect('/Users/姓名/dbs/first.db') #获取cursor对象 cur=con.cursor() #执行sql创建表 sql='create table t_person(pno INTEGER PRIMARY KEY autoincrement ,pname varchar(30) not null,age INTEGER)' try: cur.execute(sql) print('创建表成功') except Exception as e: print(e) print('创建表失败') finally: #关闭游标 cur.close() #关闭连接 con.close()
例2:插入数据
#导入sqlite3模块 import sqlite3 #硬盘上创建连接 con=sqlite3.connect('/Users/姓名/dbs/first.db') #获取cursor对象 cur=con.cursor() #执行sql创建表 sql='insert into t_person(pname,age)values(?,?)' try: cur.execute(sql,('张三',23)) #如果传递的是多个值。cur.executemany(sql,[(),(),()]) #提交事务 con.commit() print('插入数据成功') except Exception as e: print(e) print('插入失败') con.rollback() finally: #关闭游标 cur.close() #关闭连接 con.close()
例3:查找数据
#导入sqlite3模块 import sqlite3 #硬盘上创建连接 con=sqlite3.connect('/Users/姓名/dbs/first.db') #获取cursor对象 cur=con.cursor() #执行sql创建表 sql='select * from t_person' try: cur.execute(sql) #获取所有数据 person_all=cur.fetchall() print(person_all) for i in person_all: print(i) print('查询数据成功') except Exception as e: print(e) print('查询失败') finally: #关闭游标 cur.close() #关闭连接 con.close()

例4:修改数据
#执行sql创建表 update_sql='update t_person set pname=? where pno=?' try: cur.execute(update_sql,('小明',1)) #提交事务 con.commit() print('修改成功') except Exception as e: print(e) print('查询失败') con.rollback()
例5:删除数据
#执行sql创建表 sql='delete from t_person where pno=?' try: cur.execute(sql,(1,)) #提交事务 con.commit() print('删除成功')
二、pymysql模块
例6:创建表
import pymysql #创建与数据库的连接 conn=pymysql.connect(user='root',passwd='Lswtest@123',db='test_demo01',port=3306,host='127.0.0.1',charset='utf8') #创建游标对象 cur=conn.cursor() #编写创建表的sql sql=""" create table t_student( sno int primary key auto_increment, sname varchar(30) not null, age int(2), score float(3,1) ) """ try: #执行创建表的sql cur.execute(sql) print('创建成功') except Exception as e: print(e) print('创建失败') finally: #关闭游标 cur.close() #关闭连接 conn.close()
例7:插入数据
#编写创建表的sql sql="insert into t_student(sname,age,score)values(%s,%s,%s)" try: #执行创建表的sql cur.execute(sql,('小其',19,99.9)) #提交事务 conn.commit() print('插入成功') except Exception as e: print(e) conn.rollback() print('插入失败')
如果传递的是多个值。cur.executemany(sql,[(),(),()])
例8:查询数据
#编写创建表的sql sql="select * from t_student" try: #执行创建表的sql cur.execute(sql) #处理结果集 students=cur.fetchall() print(students) for i in students: print(i) print('查询成功') except Exception as e: print(e) print('查询失败')
例9:修改数据
#编写创建表的sql sql="update t_student set sname=%s where sno=%s" try: #执行创建表的sql cur.execute(sql,('张三丰',1)) conn.commit() print('修改成功') except Exception as e: print(e) conn.rollback() print('修改失败')
例10:删除数据
#编写创建表的sql sql="delete from t_student where sname=%s" try: #执行创建表的sql cur.execute(sql,'张三丰') conn.commit() print('删除成功') except Exception as e: print(e) conn.rollback() print('删除失败')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!