使用 SELECT 查询 sqlite 的分页问题 – WHERE – LIKE – LIMIT -OFFSET

Pagination Problem querying sqlite with SELECT – WHERE – LIKE – LIMIT -OFFSET

我正在尝试构建一个小型私人应用程序以更好地组织我们的研讨会。 我在编号的盒子里有很多零件。所有信息都进入 sqlite.db。 图像是单独存储的——只有图像路径存储在数据库中。 现在我正在尝试使用分页查询数据库。 但是我认为这行代码有错误:

limit_search=('SELECT * from KTRmini_table WHERE discription LIKE ?, (search_term,) LIMIT ') + str(offset) + ',' + str(limit)

如果有人能给我提示要更改什么,那将是非常友好的。 谢谢。

那是代码的相关部分。

def find_search_term_and_total_number_of_matches_if_any(self, search_term):
    search_term = self.search_entry.get()
    search_term = ("%"+search_term+"%")
    conn=sqlite3.connect("KTRmini.db")
    c = conn.cursor() 
    c.execute("SELECT count(*) FROM KTRmini_table WHERE discription LIKE ?", (search_term,))
    total_number_of_matches=c.fetchone()[0]
    print("Found " + str(total_number_of_matches) + " Matches in Database")
    limit = 5;
                    
    class Matches_window(tk.Toplevel):
        def __init__(self, parent):
            super().__init__(parent)
            self.title('KTR-Matches')
            self.configure(background='#587a8f')
            self.geometry("1280x720")
            self.iconbitmap("icon.ico")
            
    Startpage.withdraw(self)
    matches_window = Matches_window(self)
    matches_window.grab_set()

    def paged_matches(offset):
        limit_search=('SELECT * from KTRmini_table WHERE discription LIKE ?, (search_term,) LIMIT ') + str(offset) + ',' + str(limit)
        r_set=c.execute(limit_search)
        i=0
                  
        for each_match in r_set:
            for number_of_database_columns in range(len(each_match)):
                e = Entry(Matches_window, width=10, fg='blue', font=('Arial', 16)) 
                e.grid(row=i, column=j)
                e.insert(END, each_match[j])
                b = Button(text= "Button", height=1, font=('Arial', 10))
                b.grid(row=i, column=j+1)
            i=i+1
            
    paged_matches(0) # initial offset of 0

这是错误信息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\Marcel\Documents\MARCEL\PYTHON_PROJECTS\gui\KTR_mini\KTRmini_220215_028 Pagination.py", line 93, in find_search_term_and_total_number_of_matches_if_any
    paged_matches(0) # initial offset of 0
  File "C:\Users\Marcel\Documents\MARCEL\PYTHON_PROJECTS\gui\KTR_mini\KTRmini_220215_028 Pagination.py", line 81, in paged_matches
    r_set=c.execute(limit_search)
sqlite3.OperationalError: near ",": syntax error

execute() 的语法是 execute(SQL_string, *args),其中 SQL_string 是 SQL 语句,*args 是可选参数。

将参数 search_term 放在 SQL 语句中是不正确的。

正确的语法是:

limit_search = 'SELECT * FROM KTRmini_table WHERE discription LIKE ? LIMIT ? OFFSET ?'
r_set = c.execute(limit_search, (search_term, limit, offset))