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.
db = sqlite3.connect("%s.db" % self.name)selfparameter in theconnectmethod. Should be:def connect(self, name):.self.name. Just choose the one you want to keep.