0

我有一个从 SQLite3 数据库填充的 wx.Grid。用户在文本控件中输入数据,然后单击“提交值”按钮。这些值被写入数据库,然后函数将数据库加载到网格中。数据库有一个索引,它从“1”开始。该表如下所示:

创建表Cyclesindex整数、 First整数、 Second 文本、 Third整数、 Fourth 整数、 Fifth整数、 Sixth整数、 Seventh 整数、 Eighth 整数、主键(index

加载数据库时,列号会跳转并从“0”而不是“1”开始,第一行值填充“1”行,而“0”行为空白。如果我将 onLoadTable() 中的行号定义更改为“rowNumb = row +1”,那么当单击“提交值”按钮时,行从“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。

4

1 回答 1

1

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

self.grid.SetCellValue(row_num-1, i, str(cells[i]))
于 2017-04-27T16:04:16.973 回答