Re: Cursor Not Passed Properly Inter-Module

William J Chapman <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
Rich,

My observations:

You seen to be wanting to call methods on class DBtools without first
creating an instance.  You appear to call the DBtools constructor (i.e.,
DBtools().CreateDB(...))  in OnNewMod() but don't save a reference.
Then in OnSaveMod() you invoke DBtools.cur.exectue(...), but you don't
appear to specify the instance of DBtools to use.

Bill
-- 
William J. Chapman
1209 N. Guam St.
Ridgecrest CA 93555
760-371-5501


On Tue, 2006-11-28 at 16:55 -0800, Rich Shepard wrote:
> 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
> 
> 
>
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.