python操作mysql数据库

python 复制代码
# 连接数据库
mysql -u root -p
#u是用户名 p需要用密码登录数据库

# 查看数据库
show database;

# 选择数据库
use database_name;

#查看数据库当中的table表
show tables;

#创建数据库
create database examination;

#删除数据库
drop table examination;
python 复制代码
#创建表
create TABLE house (
  id int(11) unsigned NOT NULL AUTO_INCREMENT,
  price decimal(10,2) DEFAULT NULL,
  unit varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  area varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  direction varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  floor varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  layout varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

#查看表结构
desc house
#查看建表语句
show create table house
python 复制代码
# 创建表
create table class(
    id int(11) unsigned not null auto_increment,
    name varchar(80) not null,
    primary key(id)
);
create table exam(
    id int(11) unsigned not null auto_increment,
    name varchar(80) not null,
    primary key(id)
);
create table score(
    id int(11) unsigned not null auto_increment,
    student_id int(11) not null,
    subject_id int(11) not null,
    exam_id int(11) not null,
    score int(11) not null,
    primary key(id)
);
create table student(
    id int(11) unsigned not null auto_increment,
    class_id int(11) not null,
    sex char(10) not null,
    name varchar(80) not null,
    primary key(id)
);
create table subject(
    id int(11) unsigned not null auto_increment,
    name varchar(80) not null,
    primary key(id)
);

#插入数据
insert into class(name)
values('高二九班');

#修改数据
update class set name = '环境18-1'
where id = '3';

使用Python操作数据库

python 复制代码
import pymysql
python 复制代码
DATABASE = {
    'host':'127.0.0.1',
    'database':'examination',
    'user':'root',
    'password':'123qwe'
#     'charset':'utf8mb4'
}
#连接到数据库
# conn = pymysql.connect(host = '127.0.0.1',user = 'root',password = '123qwe', db = 'examination')
conn = pymysql.connect(**DATABASE)

查询操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql查询
sql = "select * from class;"
cursor.execute(sql)
#获取查询结果
results = cursor.fetchall()
#打印查询结果
for row in results:
    print(row)

插入操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql插入
sql = "insert into class(name) values('高一二十一班');"
cursor.execute(sql)
sql = "insert into class(name) values('信息18-2');"
cursor.execute(sql)
#提交sql插入语句
conn.commit()

查询操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql查询
sql = "select * from class;"
cursor.execute(sql)
#获取查询结果
results = cursor.fetchall()
#打印查询结果
for row in results:
    print(row)
复制代码
(1, '高二九班')
(2, '高三十七班')
(3, '环境18-1')
(4, '高一二十一班')
(5, '信息18-2')

更新操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql更新
sql = "update class set name = '初二十四班' where id = 4;"
cursor.execute(sql)
#提交sql更新语句
conn.commit()

查询操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql查询
sql = "select * from class;"
cursor.execute(sql)
#获取查询结果
results = cursor.fetchall()
#打印查询结果
for row in results:
    print(row)
复制代码
(1, '高二九班')
(2, '高三十七班')
(3, '环境18-1')
(4, '初二十四班')
(5, '信息18-2')

删除操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql删除
sql = "delete from class where id = 4;"
cursor.execute(sql)
#提交sql删除语句
conn.commit()

查询操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql查询
sql = "select * from class;"
cursor.execute(sql)
#获取查询结果
results = cursor.fetchall()
#打印查询结果
for row in results:
    print(row)
复制代码
(1, '高二九班')
(2, '高三十七班')
(3, '环境18-1')
(5, '信息18-2')

更新操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql更新
sql = "update class set name = '高一二十一班',id = 4 where id = 5;"
cursor.execute(sql)
#提交sql更新语句
conn.commit()

查询操作

python 复制代码
#创建游标
cursor = conn.cursor()
#执行sql查询
sql = "select * from class;"
cursor.execute(sql)
#获取查询结果
results = cursor.fetchall()
#打印查询结果
for row in results:
    print(row)
复制代码
(1, '高二九班')
(2, '高三十七班')
(3, '环境18-1')
(4, '高一二十一班')
python 复制代码
#关闭游标和连接
cursor.close()
conn.close()
python 复制代码
# 捕捉异常
try:
    a = 10
    b = a+'hello'
except TypeError as e:
    print(e)
复制代码
unsupported operand type(s) for +: 'int' and 'str'
python 复制代码
a = 10
b = a+'hello'
复制代码
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-71-00244769db33> in <module>
      1 a = 10
----> 2 b = a+'hello'


TypeError: unsupported operand type(s) for +: 'int' and 'str'
python 复制代码
#实现回滚操作(数据库操作回滚失败)
try:
    #执行sql查询
    sql = "insert into class(name) values('管科24-1');"
    #创建游标
    cursor = conn.cursor()
    #执行sql
    cursor.execute(sql)
    
    #do something
    error = 10 + 'hello'
    
    #提交sql插入语句
    conn.commit()
except Exception as e:
    print(e)
    conn.rollback()
相关推荐
深度学习入门10 分钟前
机器学习,深度学习,神经网络,深度神经网络之间有何区别?
人工智能·python·深度学习·神经网络·机器学习·机器学习入门·深度学习算法
Think Spatial 空间思维27 分钟前
【HTTPS基础概念与原理】TLS握手过程详解
数据库·网络协议·https
逝水如流年轻往返染尘34 分钟前
MySQL表的增删查改
mysql
laowangpython1 小时前
MySQL基础面试通关秘籍(附高频考点解析)
数据库·mysql·其他·面试
森哥的歌1 小时前
Python uv包管理器使用指南:从入门到精通
python·开发工具·uv·虚拟环境·包管理
mooyuan天天1 小时前
SQL注入报错“Illegal mix of collations for operation ‘UNION‘”解决办法
数据库·web安全·sql注入·dvwa靶场·sql报错
qq_214782611 小时前
给你的matplotlib images添加scale Bar
python·数据分析·matplotlib
Johny_Zhao1 小时前
Vmware workstation安装部署微软SCCM服务系统
网络·人工智能·python·sql·网络安全·信息安全·微软·云计算·shell·系统运维·sccm
waterHBO1 小时前
python + flask 做一个图床
python
运维-大白同学1 小时前
go-数据库基本操作
开发语言·数据库·golang