wxGrid 行号不与 SQLite3 数据库同步

wxGrid row numbers don't sync with SQLite3 database

我有一个从 SQLite3 数据库填充的 wx.Grid。用户在文本控件中输入数据,然后单击 "Commit Values" 按钮。这些值被写入数据库,然后该函数将数据库加载到网格中。数据库有一个索引,它从“1”开始。 table 看起来像这样:

创建 TABLE Cycles ( index 整数, First 整数, Second 文字, Third 整数, Fourth 整数, Fifth 整数, Sixth 整数, Seventh 整数, Eighth 整数, 主键(index)

当数据库加载时,列号跳转并从“0”而不是“1”开始,第一行值填充“1”行,而“0”行留空。如果我将 onLoadTable() 中的行号定义更改为 'rowNumb = row +1',那么,当单击 "Commit Values" 按钮时,行从“1”开始,但值填充第二行,留下行“1”空白的。 这是功能齐全、可重现的代码:

import wx
import wx.lib.scrolledpanel as scrolled
import sqlite3 as sqlite
import wx.grid as gridlib


class MasterFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,"",size=(900,400))

        panel = wx.Panel(self, -1)

        self.db = db
        self.c = self.db.conn.cursor()

        vbox = wx.BoxSizer(wx.VERTICAL)

        self.grid = wx.grid.Grid(panel)
        rowN = self.c.execute('SELECT Count(*) FROM Cycles')
        val = rowN.fetchone()
        rowNum = val[0] + 3
        self.grid.CreateGrid(rowNum,8)
        self.grid.Scroll(0,0)

        self.enterBtn = wx.Button(panel, -1, "Commit Values")
        self.Bind(wx.EVT_BUTTON, self.onEnter, self.enterBtn)

        vbox.Add(self.enterBtn)
        vbox.Add(self.grid)
        panel.SetSizer(vbox)
        panel.Fit()

    def onEnter(self, event):
        First = 1
        Second = 'mine'
        Third = 1
        Fourth = .5
        Fifth = 1
        Sixth = 20
        Seventh = 4
        Eighth = 10

        self.c.execute("INSERT INTO datatable VALUES (NULL,?,?,?,?,?,?,?,?);",(First,Second,Third,Fourth,Fifth,Sixth,Seventh,Eighth))
        self.db.conn.commit()
        self.onLoadTable()

    def onLoadTable(self):       
        self.grid.ClearGrid()      
        for row in range(12):
            rowNumb = row #+ 1
            self.grid.SetRowLabelValue(row, "%s" %rowNumb)

        meta = self.c.execute('SELECT * from datatable') 
        labels = []
        for i in meta.description:
            labels.append(i[0])
        labels = labels[1:]
        for i in range(len(labels)):
            self.grid.SetColLabelValue(i, labels[i])

        all = self.c.execute('SELECT * from datatable')
        for row in all:
            row_num = row[0]
            cells = row[1:]
            for i in range(len(cells)):
                if cells[i] != None and cells[i] != "null":
                    self.grid.SetCellValue(row_num, i, str(cells[i]))
        self.db.conn.commit()

class GetDatabase():
    def __init__(self):
        self.conn = sqlite.connect("data4.db") 

if __name__ == "__main__":
    db=GetDatabase()
    app = wx.App(False)
    frame = MasterFrame()
    frame.Show()
    app.MainLoop()

如果我将 ...WHERE index=%d" %rowNumb 添加到 SELECT 语句中,我会得到 sqlite3.OperationalError: near "index": syntax error.

网格从 (0,0) 开始,所以尝试从 row_num 中减去 1 即

self.grid.SetCellValue(row_num-1, i, str(cells[i]))