wxGrid行号不与SQLite3数据库同步

时间:2017-04-26 17:21:25

标签: sqlite wxpython wxgrid

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

CREATE TABLE Cycles(     index整数,     First整数,     Second文字,     Third整数,     Fourth整数,     Fifth整数,     Sixth整数,     Seventh整数,     Eighth整数,     PRIMARY KEY(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()

如果我向SELECT语句添加... WHERE index =%d“%rowNumb我得到sqlite3.OperationalError:near”index“:语法错误。

1 个答案:

答案 0 :(得分:1)

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

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