python操作mysql数据库_python进⾏MySQL数据库信息修改python对MySQL数据库信息修改⽅法
python修改MYSQL中数据信息
更新操作⽤于更新数据表的的数据,以下实例将 EMPLOYEE 表中的 SEX 字段为 'M' 的 AGE 字段递增 1:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
# 打开数据库连接
db = t("localhost","testuser","test123","TESTDB" )
# 使⽤cursor()⽅法获取操作游标
cursor = db.cursor()
# SQL 更新语句
sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
try:
# 执⾏SQL语句
# 提交到数据库执⾏
dbmit()
except:
# 发⽣错误时回滚
# 关闭数据库连接
db.close()
python实现的MySQL增删改查操作实例⼩结
本⽂实例总结了python实现的MySQL增删改查操作。分享给⼤家供⼤家参考,具体如下:
代码⽚段⼀
连接并执⾏sql
#encoding:UTF-8
import MySQLdb
conn = MySQLdb.Connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd='123456',
db='imooc',
charset='utf8'
)
cursor = conn.cursor()
print conn
print cursor
sql = "select * from user"
取数据
wcount
#取数据
#fetchone()获取⼀条数据
#fetchany(3)获取多条
#fetchall()#获取客户缓冲区的所有数据
# rs = cursor.fetchone()
# print rs
#
# rs = cursor.fetchmany(2)
# print rs
#
# rs = cursor.fetchall()
# print rs
# rs = cursor.fetchall()
# for row in rs:
# print "userid=%s,username=%s" % row
更新数据库
# sql_insert = "insert into user(userid,username) values(10,'name10')" # sql_update = "update user set username='name91' where userid=9" # sql_delete = "delete from user where userid<3"
# ute(sql_insert)
# ute(sql_update)
# ute(sql_delete)
# #执⾏完后提交
# connmit()
# #发⽣异常时回滚
# try:
# sql_insert = "insert into user(userid,username) values(10,'name10')" # sql_update = "update user set username='name91' where userid=9" # sql_delete = "delete from user where userid<3"
# ute(sql_insert)
# ute(sql_update)
# ute(sql_delete)
# connmit()
# except Exception as e:
# print e
# llback()
cursor.close()
conn.close()
代码⽚段2 银⾏实例
#coding:UTF-8
import sys
import MySQLdb
class TransferMoney(object):
def __init__(self,conn):
< = conn
def tranfer(self,source_acctid,target_acctid,money):
try:
self.check_acct_available(source_acctid)
python新手代码userid
self.check_acct_available(target_acctid)
self.has_enough_money(source_acctid,money)
self.add_money(target_acctid,money)
except Exception as e:
raise e
def check_acct_available(self, acctid):
cursor = ursor()
try:
sql = "select * from account where acctid=%s"%acctid
rs = cursor.fetchall()
if len(rs)!=1:
raise Exception("账号%s不存在"%acctid)
finally:
cursor.close()
def has_enough_money(self, acctid, money):
cursor = ursor()
try:
sql = "select * from account where acctid=%s and money>%s" % (acctid,money) ute(sql)
print "has_enough_money:"+sql
rs = cursor.fetchall()
if len(rs) != 1:
raise Exception("账号%s没有⾜够的钱" % acctid)
finally:
cursor.close()
def reduce_money(self, acctid, money):
cursor = ursor()
try:
sql = "update account set money=money-%s where acctid=%s" % (money,acctid) ute(sql)
print "reduce_money:"+sql
wcount != 1:
raise Exception("账号%s减款失败" % acctid)
finally:
cursor.close()
def add_money(self, acctid, money):
cursor = ursor()
try:
sql = "update account set money=money+%s where acctid=%s" % (money, acctid) ute(sql)
print "reduce_money:" + sql
wcount != 1:
raise Exception("账号%s加款失败" % acctid)
finally:
cursor.close()
if __name__ == "__main__":
source_acctid = sys.argv[1]
target_acctid = sys.argv[2]
money = sys.argv[3]
conn = MySQLdb.Connect(
host='127.0.0.1',
port=3306,
user='root',
passwd='123456',
db='imooc',
charset='utf8'
)
tr_money = TransferMoney(conn)
try:
anfer(source_acctid,target_acctid,money)
except Exception as e:
print "出现问题了" + str(e)
finally:
conn.close()
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python+MySQL数据库程序设计⼊门教程》、《Python常见数据库操作技巧汇总》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使⽤技巧总结》、《Python字符串操作技巧汇总》、《Python⼊门与进阶经
典教程》及《Python⽂件与⽬录操作技巧汇总》
希望本⽂所述对⼤家Python程序设计有所帮助。
以上就是本次给⼤家分享的关于java的全部知识点内容总结,⼤家还可以在下⽅相关⽂章⾥到相关⽂章进⼀步学习,感谢⼤家的阅读和⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。