Inserting in a python sqlite3 database by a class

Asked

Viewed 268 times

0

I am doing a mini project of a registration in a database sqlite3 by python, my problem is that in my method, which registers the product with (id, name, purchase price, sale price, quantity), gives this error:

self.cursor.commit() Attributeerror: 'sqlite3.Cursor' Object has no attribute 'commit'

This is my def that should include the product in the comic:

def savebd(self):
        self.cursor.execute(f"INSERT INTO produtos VALUES('{str(self.id)}',' {self.nome}',' {str(self.precodc)}',' {str(self.precodv)}',' {str(self.quantid)} ')")
        self.cursor.commit()
        print('Inserido com sucesso!')

The rest of the code is all right, the problem is only to associate this method with the syntax or right way to register this product in the bank.

To reproduce this error:

import sqlite3
banco = sqlite3.connect('bancoProd.db')
cursor = banco.cursor()
cursor.execute("CREATE TABLE produtos (id, nome, precodc, precodv, quantidade )")


class produtos():
    seq = 0
    obejects = []

    def __init__(self, id, nome, precodc, precodv, quantid):
        self.banco = sqlite3.connect('bancoProd.db')
        self.cursor = self.banco.cursor()
        self.id = None
        self.nome = nome
        self.precodc = precodc
        self.precodv = precodv
        self.quantid = quantid


    def savebd(self):
        self.__class__.seq += 1
        self.id = self.__class__.seq
        self.cursor.execute(f"INSERT INTO produtos VALUES('{str(self.id)}',' {self.nome}',' {str(self.precodc)}',' {str(self.precodv)}',' {str(self.quantid)} ')")
        self.cursor.commit()
        print('Inserido com sucesso!')


c = produtos(0, 'limpol', 1.50, 2.80, 10)
c.savebd()

people who know more there...

1 answer

0


In savedb, instead of:

self.cursor.commit()

Try:

self.banco.commit()

Cursor does not have the attribute commit()


Edit

To avoid the error that the database returns saying that the table already exists you can put this query.

In place of:

cursor.execute("CREATE TABLE produtos (id, nome, precodc, precodv, quantidade )")

Place:

cursor.execute("CREATE TABLE IF NOT EXISTS produtos (id, nome, precodc, precodv, quantidade )")

Thus the table is only created if it does not exist.

  • ok, now go to the print of 'successfully inserted! ', but it does not appear in my database yet ;-;

  • Andrey, I took the test here and was saved. I tested as follows, out of function same: cursor.execute('select * from produtos')
response = cursor.fetchall()
print(response). Exit: [('1', ' limpol', ' 1.5', ' 2.8', ' 10 ')]

  • Our brother thanks, is that I was looking at a browser half bugged, but now ta everything all right, Rigadão my good, solved!

  • This is Andrey, glad you solved it! If the answer has solved your problem, consider marking the answer as valid (not mandatory but good practice for future users with the same problem). See how. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.