加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

pymysql增删改查操作

发布时间:2020-12-20 10:11:48 所属栏目:Python 来源:网络整理
导读:表结构 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 '保密'

表结构

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,'小月月',1),'彭于晏',29,185.00,'刘德华',59,175.00,'黄蓉',38,160.00,'凤姐',28,150.00,4,'王祖贤',172.00,'周杰伦',36,170,'程坤',27,181.00,'刘亦菲',25,166.00,'金星',33,162.00,3,'静香',12,'郭靖',170.00,'周杰',34,176.00,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',
'charset': 'utf8'
}

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

?

3.修改

import pymysql

mysqlConfig = {
'host': 'localhost',
'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',
'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',1)'''
# 插入10条数据
for i in range(10):
# 执行SQL语句
row_count = cur.execute(sql_str)
# 在执行增删改操作时,如果不想提交前面的修改操作,可以使用 rollback 回滚取消操作
conn.rollback()
# 关闭游标
cur.close()
# 关闭数据库
conn.close()

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读