0
import pyodbc

class Database(object):

    def connect(self):
        connection = pyodbc.connect("""DRIVER={SQL Server};
                                       SERVER=XX\SQLEXPRESS;
                                       DATABASE=ACCOUNT_DBF;
                                       UID=sa;PWD=XXX""")
        cursor = connection.cursor()

    def check_account(self, usr):
        cursor.execute("SELECT * FROM ACCOUNT_TBL WHERE account = ?", usr)
        row = cursor.fetchone()

        print(row[0])

database = Database()
database.check_account("developer")

So, as you can see I am trying to call the "check_account" function with the parameter of "developer". But whenever I execute/build it, it gives me an error of

"NameError: name cursor not defined"

I am curious and new to python on how to actually do it. I've been searching around the web but I cannot find a specific answer for my problem.

*I am using the latest python btw(3.6.1).

4
  • But you never call connect, and even if you did you never return the cursor from that method or assign it to in instance variable, so it is lost. Commented Apr 6, 2017 at 8:24
  • you have not pass cursor object as a parameter to check_account function Commented Apr 6, 2017 at 8:25
  • @DanielRoseman your comment is not helping me sir. But thank you though. Commented Apr 6, 2017 at 8:28
  • @Surajano hi thank you for your comment. I do not think I need to pass it in the check_account function. Commented Apr 6, 2017 at 8:29

2 Answers 2

2

The NameError exception is triggered because in the check_account method you can not see local variables defined in connect method.

You need to set instance attributes "inside" self, since you can access self from all methods (it is the instance itself).

def connect(self):
    self.connection = pyodbc.connect("""DRIVER={SQL Server};
                                   SERVER=XX\SQLEXPRESS;
                                   DATABASE=ACCOUNT_DBF;
                                   UID=sa;PWD=XXX""")
    self.cursor = connection.cursor()

def check_account(self, usr):
    self.cursor.execute("SELECT * FROM ACCOUNT_TBL WHERE account = ?", usr)
    row = self.cursor.fetchone()

    print(row[0])
Sign up to request clarification or add additional context in comments.

4 Comments

This fixed it. Thank you so much for the clear answer!
I would like to add another question to you regarding the "self." is it good to add "self." everytime in the class scope?
They are different things... When you define a function inside a class by default it is an instance method and every time that function is called it is passed the self first argument where you have the object itself. If you really want to have class methods you have to use @classmethod decorator and the first argument passed will be the Class, not the instance.
@JakeMcLaren Also notice that there is a huge difference in doing a class method and an instance method. My solution is using instance methods, so when you create a Database object, each object will have its own connection. If you do this as a class method and you have a connection class attribute, the same connection is shared for all the database objects. It depends on what you really want to do ;)
0

Try:

class Database(object):

def connect(self):
    connection = pyodbc.connect("""DRIVER={SQL Server};
                                   SERVER=XX\SQLEXPRESS;
                                   DATABASE=ACCOUNT_DBF;
                                   UID=sa;PWD=XXX""")
    self.cursor = connection.cursor()

In your code, cursoris variable which below to method, only self.cursor can store cursor to class, and then other methods can use.

1 Comment

Hi there thank you for the comment. Indeed that will fix the cursor issue but. It will argue about the connection variable not being defined too. So what I did was put "self." in every variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.