Erlo

pymysql增删改查操作

2019-11-06 20:30:33 发布   326 浏览  
页面报错/反馈
收藏 点赞

表结构

CREATE TABLE `students` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT '',
`age` tinyint(3) unsigned DEFAULT '0',
`height` decimal(5,2) DEFAULT NULL,
`gender` enum('男','女','中性','保密') DEFAULT '保密',
`cls_id` int(10) unsigned DEFAULT '0',
`is_delete` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO students VALUES
(0,'小明',18,180.00,2,1,0),
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'黄蓉',38,160.00,2,1,0),
(0,'凤姐',28,150.00,4,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,170,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'静香',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);

 

1.查询

import pymysql

mysqlConfig = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'root',
'db': 'test3',
'charset': 'utf8'
}

# 连接数据库
conn = pymysql.connect(**mysqlConfig)
# 创建游标
# 设置游标类型,默认游标类型为元组形式
# self.cursor = db.cursor()
# 设置游标类型,将游标类型设置为字典形式
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 以字符串形式书写SQL语句,因为SQL语句中也会出现字符串,所以建议使用 ```引号形式将SQL诗句引起来
sql_str = '''SELECT * FROM students;'''
# 执行SQL语句
row_count = cursor.execute(sql_str)
# 显示执行 SQL 语句影响的行数
print('-' * 10, '影响的行数', '-' * 10)
print(row_count)
# 获取一条记录
print('-' * 10, '获取一条记录', '-' * 10)
row_one = cursor.fetchone()
# 显示获取的记录
print(row_one)
# 获取多条记录
print('-' * 10, '取多条记录', '-' * 10)
row_many = cursor.fetchmany(4)
# 遍历输出所有的结果
for t in row_many:
print(t)
# 获取所有的数据
print('-' * 10, '获取所有的数据', '-' * 10)
row_all = cursor.fetchall()
# 遍历输出所有的结果
for t in row_all:
print(t)
# 关闭游标
cursor.close()
# 关闭数据库
conn.close()

 

2.增加

import pymysql

mysqlConfig = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'root',
'db': 'test3',
'charset': 'utf8'
}

# 连接数据库
conn = pymysql.connect(**mysqlConfig)
# 获取游标
cur = conn.cursor()
# 以字符串形式书写SQL语句
sql_str = '''insert into students values(0,'新来的',20,180,'男',1,1)'''
# 执行SQL语句
row_count = cur.execute(sql_str)
# 在执行增删改操作时,需要向数据库提交操作,否则操作不成功
conn.commit()
# 关闭游标
cur.close()
# 关闭数据库
conn.close()

 

3.修改

import pymysql

mysqlConfig = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'root',
'db': 'test3',
'charset': 'utf8'
}

# 连接数据库
conn = pymysql.connect(**mysqlConfig)
# 获取游标
cur = conn.cursor()
# 以字符串形式书写SQL语句
sql_str = '''update students set name = '王钢蛋' where name = '新来的'; '''
# 执行SQL语句
row_count = cur.execute(sql_str)
# 在执行增删改操作时,需要向数据库提交操作,否则操作不成功
conn.commit()
# 关闭游标
cur.close()
# 关闭数据库
conn.close()

 

4.删除

import pymysql

mysqlConfig = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'root',
'db': 'test3',
'charset': 'utf8'
}

# 连接数据库
conn = pymysql.connect(**mysqlConfig)
# 获取游标
cur = conn.cursor()
# 以字符串形式书写SQL语句
sql_str = '''delete from students where name='王钢蛋'; '''
# 执行SQL语句
row_count = cur.execute(sql_str)
# 在执行增删改操作时,需要向数据库提交操作,否则操作不成功
conn.commit()
# 关闭游标
cur.close()
# 关闭数据库
conn.close()

 

5.回滚(取消操作)

import pymysql

mysqlConfig = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': 'root',
'db': 'test3',
'charset': 'utf8'
}

# 连接数据库
conn = pymysql.connect(**mysqlConfig)
# 获取游标
cur = conn.cursor()
# 以字符串形式书写SQL语句
sql_str = '''insert into students values(0,'新来的',20,180,'男',1,1)'''
# 插入10条数据
for i in range(10):
# 执行SQL语句
row_count = cur.execute(sql_str)
# 在执行增删改操作时,如果不想提交前面的修改操作,可以使用 rollback 回滚取消操作
conn.rollback()
# 关闭游标
cur.close()
# 关闭数据库
conn.close()

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认