2

I'm create simple class for SQLite datadase and when I'm insert new row table doesn't change.

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')

Interactive shell

>>> from DB import DB
>>> db = DB()
>>> db.insert('firstName', 'firstValue')
>>> print(db.getAll())
None
>>>
4
  • 3
    You need to commit when you change data. Commented Aug 12, 2016 at 11:50
  • 2
    exactly as polku says. for database "transactions" other than queries, you have to use the commit() after the execute(). In your case, it would probably be self.db.commit() at the end of the insert function Commented Aug 12, 2016 at 11:55
  • I'm added commit() , but it's still doesn't work Commented Aug 12, 2016 at 12:10
  • >>> from DB import DB >>> db = DB() >>> db.insert('firstName', 'firstValue') >>> print(DB().getAll()) None >>> Commented Aug 12, 2016 at 12:10

1 Answer 1

1

Your method getAll has no return statement. If you add it, you can see that the table actually changes:

def getAll(self):
    self.c.execute("SELECT * FROM passwords")
    return self.c.fetchall()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank! Maybe I need some break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.