图书管理系统部分代码分享

<辅助界面>
import pymysql
class admin():
def init(self, host, port, db, user, password, charset):
self.host = host
self.port = port
self.db = db
self.user = user
self.password = password
self.charset = charset
# 获取运输工具

def open_connect(self):# 找到连接对象
    self.con=pymysql.connect(self.host,
                              port=self.port,
                              db=self.db,
                              user=self.user,
                              password=self.password,
                              charset=self.charset
                              )
    self.cursor = self.con.cursor()
def insert_into(self):
    print("请进行添加操作:")
    name= input("添加的书名:")
    bauthor = input("书的作者:")
    bprice = int(input("书的价格:"))
    sql = "insert into book(bname,author,price) values('%s','%s',%s)"%(name, bauthor, bprice)

    result = self.cursor.execute(sql)
    if result > 0:
        print('添加成功')
        self.con.commit()
    else:
        print('操作失败')
def del_book(self):
    print("请进行删除操作:")
    id = int(input("要删除的书的id:"))
    sql="delete from book where bid = %s "%(id)
    print("确认删除吗?", 1, 'true',  2, 'false')
    num = input("请操作:")
    if num == "1":
        result = self.cursor.execute(sql)
        if result > 0:
            print('添加成功')
            self.con.commit()
        else:
            print('操作失败')
def update_book(self):

    print("请进行修改操作")
    id = int(input("修改的书的价格:"))
    name = input("修改的书名:")
    bauthor = input("修改的书的作者:")
    bprice = int(input("修改的书的价格:"))
    sql = "update book set (bname,author,price)=('%s','%s',%s) where bid = %s" % (name, bauthor, bprice, id)
    result = self.cursor.execute(sql)
    if result > 0:
        print('修改成功')
        self.con.commit()
    else:
        print('修改失败')
def findone(self):
    print("查找操作:")
    name = input("要查找的书的名字:")
    sql = "select * from book where bname = '%s' " % (name)
    result = self.cursor.execute(sql)
    if result > 0:
        print(self.cursor.fetchone())
        return 1
    else:
        print("没有符合的数据")
        return 0

def findone_author(self):
    print("查找操作:")
    bauthor = input("要查找的作者的名字:")
    sql = "select * from book where author = '%s' " % (bauthor)
    result = self.cursor.execute(sql)
    if result > 0:
        print(self.cursor.fetchone())
        return 1
    else:
        print("没有符合的数据")
        return 0
def findAll(self):
    sql = 'select * from book'
    result = self.cursor.execute(sql)
    if result > 0:
        print('查看成功')
        self.con.commit()
        print(self.cursor.fetchall())
    else:
        print('查看失败')
def quit(self):
    self.cursor.close()
    self.con.close()

<主界面>
import pymysql
from help import admin

hp = admin(‘localhost’, 3306, “books”, “root”, “xxx”, “utf8”)
hp.open_connect()
conn = None
conn = pymysql.connect(
host=”localhost”,
port=3306,
db=”books”,
user=”root”,
password=”xxx”,
charset=”utf8”
)
<准备cursor>
cursor= conn.cursor()

<准备sql 语句>

number=0
while number < 3:
print(‘‘*10,”图书管理登陆界面”,’‘*10)
name = input(“输入用户名:”)
password = input(“输入密码:”)
print(“ 20, “选择登陆类型:”, 1, “管理员登陆”, 2, “用户登陆”, “ 20)
type = input(“”)
if type == “1”:
sql= “select uname from users where utype =1”
cursor.execute(sql)
nm= cursor.fetchone()
# print(nm)
sql = “select upassword from users where utype =1”
cursor.execute(sql)
pwd= cursor.fetchone()
# print(str(pwd))
if nm[0] == name and pwd[0] ==password:
print(“管理员%s登陆成功.欢迎!”%(name))
while True:
print(“选择您的操作:”,
1, “添加图书信息”,
2, “删除图书信息”,
3, “修改图书信息”,
4, “根据书名查找”,
5, “查找所有信息”,
6, “退出图书管理”
)
num = input(“请选择:”)
if num == ‘1’:
hp.insert_into()
elif num == ‘2’:
hp.del_book()
elif num == ‘3’:
pass
elif num == ‘4’:
hp.findone()
elif num == ‘5’:
hp.findAll()
elif num == ‘6’:
hp.quit()
break
else:
print(“用户名或密码错误,请重新输入:”)
number += 1
continue
elif type == “2”:

    sql = "select uname from users where utype =2"
    cursor.execute(sql)
    nm = cursor.fetchone()
    # print(nm)
    sql = "select upassword from users where utype =2"
    cursor.execute(sql)
    pwd = cursor.fetchone()
    if nm[0] == name and pwd[0] == password:
        print("%s同学你好,欢迎!")
        while True:
            print("请操作:",
                  1, "根据书名查找",
                  2, '根据作者查找',
                  3, '查找所有图书',
                  4, "退出登陆"
                  )
            num = input("请输入")
            if num == '1':
                hp.findone()
            elif num == '2':
                hp.findone_author()
            elif num == '3':
                hp.findAll()
            elif num == '4':
                hp.quit()
                break
    else:
        print("用户名或密码错误,请重新输入:")
        number += 1
        continue
else:
    print("选择有误!请瞪大眼睛0.0 再次选择!")
    type = input("")
    number +=1
posted on 2018-09-08 16:46 liangzhiwen 阅读( ...) 评论( ...) 编辑 收藏
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值