目录
[TOC]
1.增
语法:
insert into 表名 valuses(),(),();
示例:
import pymysql # 实现连接mysql
# 创建连接
mysql = pymysql.connect(
host='localhost', # 连接地址, 本地
user='root', # 用户
password='111111', # 数据库密码
port=3306, # 端口,默认为3306
charset='utf8', # 编码
database='text' # 选择数据库
)
# print(mysql)
# 创建游标对象
db = mysql.cursor()
# MySQL语法
sql = 'create table if not exists student(' \
'id int not null primary key auto_increment' \
',name varchar(50) not null ' \
',age int not null ' \
',gender char(10) not null' \
');'
# 添加两条数据
append = 'insert into student(name,age,gender) values("小明",18,"男"),("小红",17,"女");'
try:
# 执行sql
db.execute(sql)
# 添加
db.execute(append)
mysql.commit() # 表示将修改操作提交到数据库
# print('创建表成功')
print('添加成功')
except Exception as e:
print('操作失败',e)
mysql.rollback() # 表示不成功则回滚数据
# 游标关闭
db.close()
# 关闭连接
mysql.close()
2.删
语法:
delete from 表名 where 删除条件;
示例:
import pymysql # 实现连接mysql
# 创建连接
mysql = pymysql.connect(
host='localhost', # 连接地址, 本地
user='root', # 用户
password='111111', # 数据库密码
port=3306, # 端口,默认为3306
charset='utf8', # 编码
database='text' # 选择数据库
)
# print(mysql)
# 创建游标对象
db = mysql.cursor()
# MySQL语法
sql = 'create table if not exists student(' \
'id int not null primary key auto_increment' \
',name varchar(50) not null ' \
',age int not null ' \
',gender char(10) not null' \
');'
# 删除数据
delete = 'delete from student where name="小明";'
try:
# 执行sql
db.execute(sql)
# 删除数据
db.execute(delete)
mysql.commit() # 表示将修改操作提交到数据库
print('删除成功')
except Exception as e:
print('操作失败',e)
mysql.rollback() # 表示不成功则回滚数据
# 游标关闭
db.close()
# 关闭连接
mysql.close()
3.改
语法:
update 表名 set 字段名=更新值,字段名=更新值,字段名=更新值… where 更新条件;
示例:
import pymysql # 实现连接mysql
# 创建连接
mysql = pymysql.connect(
host='localhost', # 连接地址, 本地
user='root', # 用户
password='111111', # 数据库密码
port=3306, # 端口,默认为3306
charset='utf8', # 编码
database='text' # 选择数据库
)
# print(mysql)
# 创建游标对象
db = mysql.cursor()
# MySQL语法
sql = 'create table if not exists student(' \
'id int not null primary key auto_increment' \
',name varchar(50) not null ' \
',age int not null ' \
',gender char(10) not null' \
');'
# 更新数据
update = 'update student set name="小芳",age=20,gender="男" where id=2;'
try:
# 执行sql
db.execute(sql)
# 更新数据
db.execute(update)
mysql.commit() # 表示将修改操作提交到数据库
print('更新成功')
except Exception as e:
print('操作失败',e)
mysql.rollback() # 表示不成功则回滚数据
# 游标关闭
db.close()
# 关闭连接
mysql.close()
4.查
语法:
select * from 表名;
示例:
import pymysql # 实现连接mysql
# 创建连接
mysql = pymysql.connect(
host='localhost', # 连接地址, 本地
user='root', # 用户
password='111111', # 数据库密码
port=3306, # 端口,默认为3306
charset='utf8', # 编码
database='text' # 选择数据库
)
# print(mysql)
# 创建游标对象
db = mysql.cursor()
# MySQL语法
sql = 'create table if not exists student(' \
'id int not null primary key auto_increment' \
',name varchar(50) not null ' \
',age int not null ' \
',gender char(10) not null' \
');'
# 查询数据
find = 'select * from student'
try:
# 执行sql
db.execute(sql)
# 查找数据
db.execute(find)
# 获取查询的所有记录
res = db.fetchall()
mysql.commit() # 表示将修改操作提交到数据库
# print('创建表成功')
print(res)
print('查找成功')
except Exception as e:
print('操作失败',e)
mysql.rollback() # 表示不成功则回滚数据
# 游标关闭
db.close()
# 关闭连接
mysql.close()