Re: Cursor Not Passed Properly Inter-Module

Rich Shepard <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
On Tue, 28 Nov 2006, Adrian Klaver wrote:

> I think I see what is happening. In your first post dba creates an
> instance of DBtools().CreateDB(). This has a cursor associated with via
> the if statement. The cursor executes the CREATE TABLE statement. In the
> OnSaveMod there is no creation of a DBtools instance. Calling
> DBTools.cur.execute fails because at the top level of the class cur=None.
> With what you have shown cur is only available through the CreateDB
> function.

   And I think that I'm following you. But, I don't see the solution. Here's
more detail.

   In the calling module, modelPage.py, two of the bound (to buttons) methods
are:

   def OnNewMod(self, event):
     newName = functions.newFile(self)
     self.tcName.SetValue(self.appData.projname)
     DBtools().CreateDB(self.appData.dbFileName)

and

   def OnSaveMod(self, event):
     self.modName = self.tcName.GetValue()
     self.modDesc = self.tcDesc.GetValue()
     self.modType = self.rb.GetString(self.rb.GetSelection())
     self.knowlEng = self.keName.GetValue()
     self.method = self.agent.GetSelection()
     DBtools.cur.execute("insert or replace into model (model_name, \
                          model_desc, model_type, \
                          keName, agent) values (self.modnName, \
                          self.modDesc, self.modType, \
                          self.knowlEng, self.method)")
     DBtools().SaveDB(self.appData.dbFileName)

   So, the first is calling a method in the class DBtools, which requires the
cursor, and the second is trying to directly call the cursor.

   Based on your message, I modified dbMethods.py so the connection and
cursor are specified in each method. For example,

   def CreateDB(self, dbFileName):
     self.con = sqlite3.connect(self.appData.dbFileName)
     self.cur = self.con.cursor()

and

   def SaveDB(self, dbFileName):
     self.con = sqlite3.connect(self.appData.dbFileName)
     self.cur = self.con.cursor()
     self.con.commit()

   But, the error shows up on the line before the call to DBtools.SaveDB(). I
read the error message telling me that the calling module does not see the
cursor in the imported module. If that's the case, I have no idea how to
make the cursor visible to the caller. In the class DBtools(), the
connection and cursor cannot be created until the database name is provided.

   Should I put the connection and cursor creation also in config.py with its
configData() class that's imported by all other modules?

Thanks,

Rich



-- 
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.