续写on_add_book,将输入的信息添加到数据库内

def on_add_book(self): # 获取界面上的信息 bookid = self.edit_add_bookid.text() bookname = self.edit_add_bookname.text() author = self.edit_add_author.text() chubanshe = self.edit_add_chubanshe.text() time = self.edit_add_time.text() price = self.edit_add_price.text() lendornot = self.edit_add_lendornot.text()

# 连接数据库
db = pymysql.connect("localhost", "root", "123456", "library")
cursor = db.cursor()

# 执行SQL语句
sql = "INSERT INTO LBook (图书ID, 书名, 作者, 出版社, 出版年月, 价格, 是否借出) \
       VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (bookid, bookname, author, chubanshe, time, price, lendornot)
try:
    cursor.execute(sql)
    db.commit()
    QMessageBox.information(self, "提示", "添加成功!")
except:
    db.rollback()
    QMessageBox.warning(self, "提示", "添加失败!")
db.close(
class Add_Book_WindowQMainWindow def __init__self parent=None superAdd_Book_Window self__init__parent selfsetWindowTitle增加图书界面 selfresize400 300 # 创建界面控件 self

原文地址: http://www.cveoy.top/t/topic/hmoS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录