【问题标题】:Python with mvc - sqlite database instead of retrieving single row by id, retrieving whole tablePython with mvc - sqlite 数据库而不是按 id 检索单行,检索整个表
【发布时间】:2021-05-12 15:44:44
【问题描述】:

我已经编写了应该使用员工 ID 从数据库中检索一些员工数据的函数。不幸的是,该函数确实检索了数据,但它检索的不是一名员工,而是整个表。我该如何解决?

型号

    def get_employee_data_by_id(self, id):
    connection = sqlite3.connect("employees_database.db")
    cursor = connection.cursor()
    cursor.execute("""
        SELECT name, surname from employees WHERE ID = id"""), (id)

    data = cursor.fetchall()

    connection.commit()
    cursor.close()
    connection.close()
    return data

控制器

    def get_employee_data_by_id(self, id):
    data = self.model.get_employee_data_by_id(id)
    print(type(data))

    for i in data:
        self.view.employee_data.append(i)
        print(i)

查看

    def show_frame1(self, event=None):
    self.main_frame.grid_forget()
    self.edit_employee_frame.grid()

    self.controller.get_employee_data_by_id(2)

【问题讨论】:

  • 使用?占位符:WHERE ID = ?

标签: python sqlite tkinter model-view-controller


【解决方案1】:

以下行(注意(id)cursor.execute(...) 之外):

cursor.execute("""
        SELECT name, surname from employees WHERE ID = id"""), (id)

应该改为(使用占位符):

cursor.execute("""SELECT name, surname from employees WHERE ID = ?""", (id,))

【讨论】:

  • 谢谢。感谢您的帮助。
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
相关资源
最近更新 更多