cashmeer
cashmeer

Reputation: 37

creating an sqlite database from an external file with a class object

I'm trying to create a database from an external sqlite file using this python class, but I'm getting this error message:

query() takes 2 positional arguments but 3 were given

I think I understand the issue, but I don't know a way around it, can someone point me in the right direction?

class DatabaseManager:
    def __init__(self, db):
        self.conn = sqlite3.connect(db)
        self.conn.commit()
        self.cur = self.conn.cursor()

    def query(self, arg):
        self.test_setup = open(arg)
        self.executescript(test_setup.read())  
        self.cur.execute(arg)
        self.con.commit()
        return self.cur

    def __del__(self):
        self.conn.close()

dbmgr = DatabaseManager("testdb.db")
dbmgr.query('test_setup.sql', 'r')

Upvotes: 1

Views: 339

Answers (2)

Simon Marcoux
Simon Marcoux

Reputation: 105

if you take a look at your declaration of queryfor your class you will see that you got two parameters: self and arg

However when you call your query function, there are three parameters that are passed: the implicit self that you don't need to mention, the db and the parameter.

If you want arg to be a multiple argument you will need to rewrite it like this : *arg. Otherwise, you can modify your query declaration for three parameters.

Keep in mind that *arg is iterable and you might need to deconstruct it before passing it to your other functions.

For further reading about *args and **kwargs you can visit this website: *args and **kwargs in python

Upvotes: 0

MSeifert
MSeifert

Reputation: 152657

Your query only takes one argument arg but you pass in self (implicitly) and 'test_setup.sql' and 'r'. Given that you don't use the 'r' you should probably only call:

dbmgr = DatabaseManager("testdb.db")
dbmgr.query('test_setup.sql')

Upvotes: 1

Related Questions