Skip to content

opencv连接数据库

连接数据库

  • 这个方法用于连接到 MySQL 数据库。
  • self.connection: 保存数据库连接对象。
  • self.cursor: 创建一个游标对象,用于执行数据库操作。
    def connect(self):
        self.connection = mysql.connector.connect(
            host=self.host,
            user=self.username,
            passwd=self.password,
            database=self.database
        )
        self.cursor = self.connection.cursor()

创建表

  • 这个方法用于创建一个名为 book 的表,如果表不存在。
  • 表有两个字段:book_cm_isbn 是主键,自动递增;book_name 是书名,最大长度为 255 字符。
    def create_table(self):
        self.connect()
        create_table_sql = """
        CREATE TABLE IF NOT EXISTS book (
            book_cm_isbn INT AUTO_INCREMENT PRIMARY KEY,
            book_name VARCHAR(255) NOT NULL
        )
        """
        self.cursor.execute(create_table_sql)
        self.disconnect()

插入数据

  • 这个方法用于向表中插入一条记录。
  • bookidbookname 是要插入的数据。
  • 使用 commit() 方法提交事务以确保数据插入到数据库中。
    def insert_data(self, bookname, bookid):
        self.connect()
        insert_sql = f"INSERT INTO {self.TABLE_NAME} (book_cm_isbn, book_name) VALUES (%s, %s)"
        val = (bookid, bookname)
        self.cursor.execute(insert_sql, val)
        self.connection.commit()
        self.disconnect()

查询所有数据

  • 这个方法用于查询表中的所有记录或根据书名查询特定记录。
  • 如果 booknameallNone,查询所有记录;否则,根据书名进行模糊查询。
    def select_all_data(self, bookname=None):
        self.connect()
        if bookname == "all" or not bookname:
            query_sql = f"SELECT * FROM {self.TABLE_NAME}"
            self.cursor.execute(query_sql)
        else:
            query_sql = f"SELECT * FROM {self.TABLE_NAME} WHERE book_name LIKE %s"
            self.cursor.execute(query_sql, (f"%{bookname}%",))
        results = self.cursor.fetchall()
        self.disconnect()
        return results

更新数据

    def update_data(self, new_bookname, bookid):
        self.connect()
        update_sql = f"UPDATE {self.TABLE_NAME} SET book_name = %s WHERE book_cm_isbn = %s"
        self.cursor.execute(update_sql, (new_bookname, bookid))
        self.connection.commit()
        self.disconnect()

删除数据

    def delete_data(self, bookid):
        self.connect()
        delete_sql = f"DELETE FROM {self.TABLE_NAME} WHERE book_cm_isbn = %s"
        self.cursor.execute(delete_sql, (bookid,))
        self.connection.commit()
        self.disconnect()

定义 PyQt 界面类

  • 在初始化时,设置窗口标题和尺寸,并调用 init_ui() 方法初始化界面。
  • 初始化数据库连接,并创建一个表。
class DatabaseApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('library')
        self.setGeometry(100, 100, 1200, 1000)
        self.init_ui()

        # 初始化数据库连接
        self.db = MySQLDatabase(host="8.147.233.239", username="root", password="team2111", database="cjc", TABLE_NAME="book_info")
        self.db.create_table()

插入数据方法

  • 从文本编辑框获取 booknamebookid,然后调用 insert_data 方法插入数据。
  • 在文本显示框中显示添加的记录信息。
    def insert_data(self):
        bookname = self.bookname_edit.text()
        bookid = self.bookid_edit.text()
        self.db.insert_data(bookname, bookid)
        self.output_text.append(f"添加: {bookname}, {bookid}")

更新数据方法

  • 从文本编辑框获取新的书名和 bookid,然后调用 update_data 方法更新数据。
  • 在文本显示框中显示更新的信息。
    def update_data(self):
        new_bookname = self.bookname_edit.text()
        bookid = self.bookid_edit.text()
        self.db.update_data(new_bookname, bookid)
        self.output_text.append(f"更新的ID: {bookid}, 新名字: {new_bookname}")

删除数据方法

  • 从文本编辑框获取 bookid,然后调用 delete_data 方法删除数据。
  • 在文本显示框中显示删除的信息。
 def delete_data(self):
        bookid = self.bookid_edit.text()
        self.db.delete_data(bookid)
        self.output_text.append(f"删除: {bookid}")

查询数据方法

  • 从文本编辑框获取 bookname,然后调用 select_all_data 方法查询数据。
  • 清除文本显示框,并显示查询结果。如果没有匹配的记录,显示“没有找到匹配的书籍”。
    def select_data(self):
        bookname = self.search_edit.text()
        results = self.db.select_all_data(bookname)
        self.output_text.clear()
        if not results:
            self.output_text.append("没有找到匹配的书籍。")
        else:
            for row in results:
                self.output_text.append(f"book_name: {row[0]} \nbook_cm_isbn: {row[2]}")