在sqlite python中插入';不要换桌子

在sqlite python中插入';不要换桌子,python,sqlite,Python,Sqlite,我正在为SQLite Database创建简单类,当我插入新行时,表不会改变 DB.py 交互式shell >>> from DB import DB >>> db = DB() >>> db.insert('firstName', 'firstValue') >>> print(db.getAll()) None >>> 您的方法getAll没有返回语句。如果添加它,可以看到表实际上发生了更改: def

我正在为SQLite Database创建简单类,当我插入新行时,表不会改变

DB.py

交互式shell

>>> from DB import DB
>>> db = DB()
>>> db.insert('firstName', 'firstValue')
>>> print(db.getAll())
None
>>>

您的方法
getAll
没有返回语句。如果添加它,可以看到表实际上发生了更改:

def getAll(self):
    self.c.execute("SELECT * FROM passwords")
    return self.c.fetchall()

你需要在更改数据时提交。正如波尔库所说。对于查询以外的数据库“事务”,必须在
execute()
之后使用
commit()
。在您的情况下,它可能是
insert
函数末尾的
self.db.commit()
,但是它仍然不起作用>>>从db import db>>>db=db()>>>db.insert('firstName','firstValue')>>>打印(db().getAll())无>>谢谢!也许我需要休息一下
def getAll(self):
    self.c.execute("SELECT * FROM passwords")
    return self.c.fetchall()