Python连接MySQL数据库方法介绍( 三 )


print ("%s %3s" % (desc[0][0], desc[1][0]))
cursor.scroll(10,mode='absolute')
results = cursor.fetchall()
for result in results:
print (result)
except:
import traceback
traceback.print_exc()
# 发生错误时会滚
conn.rollback()
finally:
# 关闭游标连接
cursor.close()
# 关闭数据库连接
conn.close()
综合案例
FIFA球员信息系统
from pymysql import *
class Mysqlpython:
def __init__(self, database='test', host='127.0.0.1', user="root",
password='password', port=3306, charset="utf8"):
self.host = host
self.user = user
self.password = password
self.port = port
self.database = database
self.charset = charset
# 数据库连接方法:
def open(self):
self.db = connect(host=self.host, user=self.user,
password=self.password, port=self.port,
database=self.database,
charset=self.charset)
# 游标对象
self.cur = self.db.cursor()
# 数据库关闭方法:
def close(self):
self.cur.close()
self.db.close()
# 数据库执行操作方法:
def Operation(self, sql):
try:
self.open()
self.cur.execute(sql)
self.db.commit()
print("ok")
except Exception as e:
self.db.rollback()
print("Failed", e)
self.close()
# 数据库查询所有操作方法:
def Search(self, sql):
try:
self.open()
self.cur.execute(sql)
result = self.cur.fetchall()
return result
except Exception as e:
print("Failed", e)
self.close()
def Insert():#如何从外面将数据录入到sql语句中
ID = int(input("请输入球员编号:"))
people_name = input("请输入球员名字:")
PAC = int(input("请输入速度评分:"))
DRI = int(input("请输入盘带评分:"))
SHO = int(input("请输入射门评分:"))
DEF = int(input("请输入防守评分:"))
PAS = int(input("请输入传球评分:"))
PHY = int(input("请输入身体评分:"))
score =(PAC+DRI+SHO+DEF+PAS+PHY)/6
sql_insert = "insert into FIFA(ID, people_name, PAC,DRI,SHO,DEF, PAS, PHY, score) values(%d,'%s',%d,%d,%d,%d,%d,%d,%d)"%(ID, people_name, PAC,DRI,SHO,DEF, PAS, PHY, score)
print(people_name)
return sql_insert
def Project():
print("球员的能力评分有:")
list=['速度','盘带','射门','防守','传球','身体','综合']
print(list)
【Python连接MySQL数据库方法介绍】def Exit():
print("欢迎下次使用!!!")
exit()
def Search_choice(num):
date = Mysqlpython()
date.open()
if num=="2":
# 1.增加操作
sql_insert = Insert()
date.Operation(sql_insert)
print("添加成功!")
Start()
elif num=="1":
# 2.查找数据,其中order by 是为了按什么顺序输出,asc 是升序输出,desc降序输出
input_date=input("请选择您想要以什么格式输出:默认升序排列1.球员编号,2.速度,3.盘带,4.射门,5.防守,6.传球,7.身体,8.综合 ")
if input_date=="1":
sql_search = "select * from FIFA order by ID asc"
elif input_date=="2":
sql_search = "select * from FIFA order by PAC asc"
elif input_date=="3":
sql_search = "select * from FIFA order by DRI asc"
elif input_date=="4":
sql_search = "select * from FIFA order by SHO asc"
elif input_date=="5":
sql_search = "select * from FIFA order by DEF asc"
elif input_date=="6":
sql_search = "select * from FIFA order by PAS asc"
elif input_date=="7":
sql_search = "select * from FIFA order by PHY asc"
elif input_date=="8":
sql_search = "select * from FIFA order by PHY score"
else:
print("请重新输入!")
result = date.Search(sql_search)
print(" 编号 姓名 速度 盘带 射门 防守 传球 身体 综合 ")
for str in result:
print(str)
Start()
elif num=="3":
Project()
Start()
elif num=="4":
del_num=input("请输入您要删除球员的编号:")
sql_delete="delete from FIFA where id=%s"%del_num
date.Operation(sql_delete)
print("删除成功!")
Start()
elif num=="5":
Exit()
else:


推荐阅读