python中管理mysql数据库

 

一、配置服务

下载数据库mysql,php,

具体查看之前博客

2

# _*_ coding:utf-8 _*_
"""
file: 02.py
date: 2018-07-25 10:17 AM
author: westos-dd
desc:
    

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

# 伸出手
cur = conn.cursor()

# 拿东西
recont = cur.execute('select * from userInfo')
data = cur.fetchall()

# 把手伸回来
cur.close()

# 把门关上
conn.close()
print recont
print data

 

# _*_ coding:utf-8 _*_
"""
file: 02.py
date: 2018-07-25 10:17 AM
author: westos-dd
desc:
    

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

# 伸出手
#cur = conn.cursor() #创建了一个“手”
cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
# 拿东西
# 这个操作影响了多少行数(有多少行被操作了)
recont = cur.execute('select * from userInfo')
data = cur.fetchall()

# 把手伸回来
cur.close()

# 把门关上
conn.close()
print recont
print data

对数据库进行增加

# _*_ coding:utf-8 _*_
"""
file: 增.py
date: 2018-07-25 10:48 AM
author: westos-dd
desc:


"""
import MySQLdb

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

# 伸出手
cur = conn.cursor()

# 操作数据
sql = 'insert into usermg(id,name,address) values(%s,%s,%s)'
params = ('1', 'dd', 'usa')
recount = cur.execute(sql, params)

# 提交请求
conn.commit()

# 把手伸回来
cur.close()

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

对数据库进行删除

# _*_ coding:utf-8 _*_
"""
file: 删.py
date: 2018-07-25 10:48 AM
author: westos-dd
desc:
    

"""
import MySQLdb

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

# 伸出手
cur = conn.cursor()

# 操作数据
sql = 'delete from usermg where id = %s'
params = (1,)
recount = cur.execute(sql,params)

# 提交请求
conn.commit()

# 把手伸回来
cur.close()

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

插入多条数据

# _*_ coding:utf-8 _*_
"""
file: 插入多条数据.py
date: 2018-07-25 11:29 AM
author: westos-dd
desc:
    

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

# 伸出手
cur = conn.cursor()

# 操作数据
# sql = 'update usermg set name = %s where id = %s'
# params = ('uu','1',)
li = [
    ('2','hahaha','wwww'),
    ('3','lalala','qqqq'),
]
recount = cur.executemany('insert into usermg(id,name,address) values(%s,%s,%s)',li)

# 提交请求
conn.commit()

# 把手伸回来
cur.close()

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

改变数据库中的内容

# _*_ coding:utf-8 _*_
"""
file: 改.py
date: 2018-07-25 10:48 AM
author: westos-dd
desc:
    

"""
import MySQLdb

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

# 伸出手
cur = conn.cursor()

# 操作数据
sql = 'update usermg set name = %s where id = %s'
params = ('uu','1',)
recount = cur.execute(sql,params)

# 提交请求
conn.commit()

# 把手伸回来
cur.close()

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

数据表的回滚

# _*_ coding:utf-8 _*_
"""
file: commit.py
date: 2018-07-25 11:34 AM
author: westos-dd
desc:
提交和回滚 在数据库里交事务操作

"""
import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='dd',db='python')
cur = conn.cursor()

sql = 'update money set money = %s where id = 1'
params = ('0',)
recount = cur.execute(sql,params)

sql = 'update money set money = %s where id = 2'
param = ('100',)
recount = cur.execute(sql,param)
conn.commit()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值