1

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

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

2 Comments

I don't use the 'r'? I'm getting more errors if I exclude it. AttributeError: 'DatabaseHandler' object has no attribute 'executescript'
@cashmeer 'r' is the default mode for open so you don't need to explicitly give it - and execute doesn't take an 'r' parameter if you don't use any parameters in your sql,
0

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

Comments

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.