python之mysql的控制

学习之前先复习一下以前所学的linux的数据库相关知识,可以去看之前我写的数据库的博客
这里只是写了此实验需要的一些基本操作  
yum install mariadb-server.x86_64 -y
systemctl start mariadb.service 
yum install gcc -y
yum install MySQL-python.x86_64 -y
连上wifi
pip install MySQL-python
打开pycharm,输入 import MySQLdb  #如果MySQLdb处没有报错就说明成功导入了

创建数据库
这里写图片描述

这里写图片描述

mysql

# _*_ coding:utf-8 _*_
"""
file:-01.py
date: 2018-07-上午1:18
author:ting
desc:
只输出改变了几行
"""
import MySQLdb
# 打开门

door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

# 伸出手
hand = door.cursor()

# 拿东西
recont = hand.execute('select * from username')

# 把手伸回来
hand.close()

# 把门关上
door.close()

print recont

这里写图片描述

# _*_ coding:utf-8 _*_
"""
file:-02.py
date: 2018-07-上午2:45
author:ting
desc:
返回内容
"""
import MySQLdb
# 打开门

door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

# 伸出手
hand = door.cursor()  # 创建了一个“手”

# 拿东西
# 这行操作影响了有多少行(有多少行被操作了)
recount = hand.execute('select * from username')
data = hand.fetchall()

# 把手伸回来
hand.close()

# 把门关上
door.close()

print recount
print data

这里写图片描述

# _*_ coding:utf-8 _*_
"""
file:-03.py
date: 2018-07-上午2:48
author:ting
desc:
字典输出
"""
import MySQLdb
# 打开门

door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

# 伸出手
# hand = door.cursor()  # 创建了一个“手”
hand = door.cursor(cursorclass=MySQLdb.cursors.DictCursor)
# 拿东西
# 这行操作影响了有多少行(有多少行被操作了)
recount = hand.execute('select * from username')
data = hand.fetchall()
# 把手伸回来
hand.close()

# 把门关上
door.close()

# 显示被操作的行数
print recount
print data

# _*_ coding:utf-8 _*_
"""
file:增.py
date: 2018-07-上午2:51
author:ting
desc:

"""
import MySQLdb

# 打开门
door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

# 伸出手
hand = door.cursor()

# 操作数据
sql = 'insert into username(id,name)values(%s,%s)'
params = ('001','wang')
recount = hand.execute(sql,params)

# 提交请求
door.commit()

# 把手伸回来
hand.close()

# 把门关上
door.close()
print recount

这里写图片描述
这里写图片描述

# _*_ coding:utf-8 _*_
"""
file:插入多条数据.py
date: 2018-07-上午4:46
author:ting
desc:

"""
import MySQLdb
# 打开门
door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

# 伸出手
hand = door.cursor()

# 操作数据
gai = [
    ('3','lily'),
    ('4','lucy'),
]
recount = hand.executemany('insert into username(id,name)values(%s,%s)',gai)

# 提交请求
door.commit()

# 把手伸回来
hand.close()

# 把门关上
door.close()

这里写图片描述

# _*_ coding:utf-8 _*_
"""
file:删.py
date: 2018-07-上午2:51
author:ting
desc:

"""
import MySQLdb

# 打开门
door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='python')

# 伸出手
hand = door.cursor(cursorclass=MySQLdb.cursors.DictCursor)

# 操作数据
sql = 'delete from username where id = %s'
ida = (003,)
recount = hand.execute(sql,ida)

# 提交请求
door.commit()

# 把手伸回来
hand.close()

# 把门关上
door.close()

print recount

这里写图片描述
这里写图片描述

commit(事务操作)

# _*_ coding:utf-8 _*_
"""
file:commit.py
date: 2018-07-上午5:49
author:ting
desc:
这个代码会正常输出,目的是和后边的不正常输出作对比
"""
import MySQLdb

# 打开门
door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='money')

# 伸出手
hand = door.cursor()

# 操作数据
sql = 'update borrow set money1 = %s where id = 001'
id1 = ('0',)
recount = hand.execute(sql,id1)
# door.commit()
sql = 'update borrow set money1 = %s where id = 002'
id2 = ('100',)
recount = hand.execute(sql,id2)

door.commit()

修改前
这里写图片描述
修改后
这里写图片描述

# _*_ coding:utf-8 _*_
"""
file:commit1.py
date: 2018-07-上午3:06
author:ting
desc:
提交和回滚  : 在数据库里叫事务操作
模拟回滚
在同一张数据表一增一减时若有一个出现错误,则两个都不会执
行,会返回原来的结果,这种操作叫做回滚
"""
import MySQLdb

# 打开门
door = MySQLdb.connect(host='127.0.0.1',user='root',passwd='westos',db='money')

# 伸出手
hand = door.cursor()

# 操作数据
sql = 'update borrow set money1 = %s where id = 001'
id1 = ('0',)
recount = hand.execute(sql,id1)

sql = 'update borrow set money1 = %s where id = 002'
# 此处的参数与上面的一致,不会报错,但是不能执行,就会出现回滚
id1 = ('100',)
recount = hand.execute(sql,id1)

door.commit()

这里写图片描述

猜你喜欢

转载自blog.csdn.net/wzt888_/article/details/81220574
今日推荐