-3

I don't know what to make of this error. How do I pass through a SQL statement without it bringing up this error? I can only use quotations for SQL statements, so I can't change the data type.

I tried reformatting the INSERT statement to no avail.

class MD:    

    def __init__(self):
        self.conn = sqlite3.connect('Ex.db')
        self.cursor = self.conn.cursor()

        self.cursor.execute('''CREATE TABLE IF NOT EXISTS Exa (
                       Ex1 INTEGER PRIMARY KEY,
                       Ex2 TEXT NOT NULL,
                       );''')
        self.conn.commit()    

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

    def execute(self, exone, extwo):
        self.cursor.execute()    

    def fetchall(self):
        self.cursor.fetchall()

    def fetchone(self):
        self.cursor.fetchone()

    def commit(self):
        self.conn.commit()    

Ex1 = tk.StringVar
Ex2 = tk.StringVar
Entry = tk.Entry(textvariable=Ex1)
Entry1 = tk.Entry(textvariable=Ex2)
MD.execute('INSERT INTO INTO Exa(Ex1, Ex2) VALUES (?, ?)', exone=Ex1, extwo=Ex2)

The error is

Traceback (most recent call last):
  File "C:/Users/S/Code/FurtherTest.py", line 42, in <module>
    MD.execute('INSERT INTO INTO Exa(Ex1, Ex2) VALUES (?, ?)', exone=Ex1, extwo=Ex2)
  File "C:/Users/S/Code/FurtherTest.py", line 25, in execute
    self.cursor.execute()
AttributeError: 'str' object has no attribute 'cursor'
TrainTirade
  • 106
  • 8
  • It seems pointless to have your `MD` class if all it's doing is creating a table. That's not the point of a class – Iain Shelvington Jul 30 '19 at 23:01
  • @IainShelvington - some people like to use classes for composition - though in Python it is more common to use modules instead of classes for composition. – wwii Jul 30 '19 at 23:06
  • @wwii that's true but in this case I think it's better to steer people that don't know about design patterns to the more explicit patterns. I should have worded my comment more carefully – Iain Shelvington Jul 30 '19 at 23:09
  • Related: [What is the purpose of the word 'self', in Python?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self-in-python) – wwii Jul 30 '19 at 23:18
  • I see. Thank you. – TrainTirade Jul 31 '19 at 13:29

1 Answers1

1

You are calling the execute method on the class (MD), the first argument in that call is being passed to the self parameter. Since the first argument is a string it causes the Exception. Make an instance of the class first:

...
md = MD()
md.execute('INSERT INTO INTO Exa(Ex1, Ex2) VALUES (?, ?)', exone=Ex1, extwo=Ex2)

By calling the method on the instance, the self parameter becomes the instance itself and the INSERT string is passed to the exone parameter.

wwii
  • 23,232
  • 7
  • 37
  • 77
  • This solved my error. Now that I've used your suggestion, I'm getting the TypeError: execute() got multiple values for argument 'exone' when running the code. – TrainTirade Jul 31 '19 at 13:39
  • @FreightFight you should ask another question. – wwii Jul 31 '19 at 14:23