在sqlite python中插入不会更改表

时间:2016-08-12 11:49:18

标签: python sqlite

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

DB.py

import sqlite3

class DB:
    def __init__(self, **kwargs):
        self.db = sqlite3.connect('passwods.db')
        self.c = self.db.cursor()
        self.c.execute('CREATE TABLE IF NOT EXISTS passwords (name, value)')

    def insert(self, alias, cipher):
        column = (alias, cipher)
        self.c.execute('INSERT INTO passwords (name, value) VALUES (?,?)',  column)
        self.db.commit()

    def get(self, alias):
        pk = (alias,)
        self.c.execute('SELECT * FROM passwords WHERE name=?', pk) 

    def getAll(self):
        self.c.execute('SELECT * FROM passwords')

交互式shell

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

1 个答案:

答案 0 :(得分:1)

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

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