# 连接数据库
mysql -u root -p
#u是用户名 p需要用密码登录数据库# 查看数据库
show database;# 选择数据库
use database_name;#查看数据库当中的table表
show tables;#创建数据库
create database examination;#删除数据库
drop table examination;
#创建表
create TABLE house (idint(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
# 创建表
create table class(idint(11) unsigned not null auto_increment,
name varchar(80)not null,
primary key(id));
create table exam(idint(11) unsigned not null auto_increment,
name varchar(80)not null,
primary key(id));
create table score(idint(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(idint(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(idint(11) unsigned not null auto_increment,
name varchar(80)not null,
primary key(id));#插入数据
insert into class(name)
values('高二九班');#修改数据
update classset name ='环境18-1'
where id='3';
# 捕捉异常try:
a =10
b = a+'hello'except TypeError as e:print(e)
unsupported operand type(s) for +: 'int' and 'str'
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'
#实现回滚操作(数据库操作回滚失败)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()