0

I have the following test code:

import sqlite3

class database:
    def __init__(self, name):
        self.name = name

    def connect(name):
        db = sqlite3.connect("%s.db" % self.name)
        c = db.cursor()

    def test(self):
        print (3)

If I run database('name').test(), I get 3, so that works. But if I try database('name').connect(), I get, "NameError: name 'sqlite3' is not defined"

Classes are relatively new to me and I'm still trying to figure out how they work. Thanks much for any help.

EDIT: See the comments in the answer the the solution. The problem seems to be that I was importing sqlite3 in both the file and the notebook. The solution was to import the module only in the file.

4
  • 1
    It does not explain your error, but I think that should be: db = sqlite3.connect("%s.db" % self.name) Commented Jul 7, 2017 at 23:24
  • you're right, thanks for pointing out Commented Jul 7, 2017 at 23:26
  • 1
    And you are missing the self parameter in the connect method. Should be: def connect(self, name):. Commented Jul 7, 2017 at 23:27
  • You are calling name in your method, but you assign self.name. Just choose the one you want to keep. Commented Jul 7, 2017 at 23:27

1 Answer 1

2

You may have a typo somewhere in your description, because I think you would get another error first when doing

data = database()
data.test()
data.connect('name')

You need to add the self to the connect def to make it a proper class method. There's also a typo in the connect call. The following should work

import sqlite3

class database:
    def __init__(self, name):
        self.name = name

    def connect(self):
        db = sqlite3.connect("%s.db" % self.name)
        c = db.cursor()

    def test(self):
        print(3)

then do

data = database('test')
data.connect()
Sign up to request clarification or add additional context in comments.

6 Comments

You're right that I had a typo in the description, which was sloppy on my part; apologies for that. But I'm still getting this same error when using that code verbatim (save for replacing 'test' with the name of my db). It keeps telling me it doesn't recognize the name sqlite3.
you are sure that you imported it in your current python session?
imported in both the file where the class is created and inside the jupyter notebook I'm running that class in. That's why it's so frustrating :(
@snapcrack I suggest restarting your Jupyter kernel. You've probably polluted your namespace somehow.
you only need to import sqlite3 in the file that defines the class, which you are then importing into your Jupyter notebook. Maybe double check that you are importing the class from the correct file, maybe you have a second one without the sqlite3 import and import the class from there by accident
|

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.