| Newsgroups |
gmane.comp.python.db.pysqlite.user |
| Message-ID |
<OF79992375.CFF6A267-ON8525721B.00584D0E-8525721B.00596FB3@bankofny.com> |
I know that this might just mess your world up all over the place, but
here is something that I did a while back, since I had multple classes
that I wanted to use a single database connection I did this:
from pysqlite2 import dbapi2 as sqlite
class SQLLite:
con = None
filename = None
DEFAULT_DATABASE = 'TV.db'
def __init__(self, filename=DEFAULT_DATABASE):
self.filename = filename
def getConnection(self, filename=DEFAULT_DATABASE):
if self.con is None:
self.con = sqlite.connect(filename)
if self.con is not None:
self.con.autocommit = 1
return self.con
else:
return None
def returnConnection(self):
if self.con is not None:
self.con.close()
self.con = None
class ChoirDBFuncs(SQLLite):
lite = None
logfilename = None
MusicTypeConf = None
def __init__(self):
global con, cur
self.lite = SQLLite('Choir.db')
self.logfilename = 'Choir.Log'
self.MusicTypeConf = 'MusicType.csv'
#print self.MusicTypeConf
## Initialize Logging
self.logger = logging.getLogger('ChoirDBFuncs')
hldr = logging.FileHandler(self.logfilename)
formatter = logging.Formatter('%(asctime)s %(levelname)s
%(message)s')
hldr.setFormatter(formatter)
self.logger.addHandler(hldr)
self.logger.setLevel(logging.DEBUG)
## Initialize Connection to Database
con = self.getActiveConnection()
cur = con.cursor()
def getActiveConnection(self, filename='Choir.db'):
if self.lite is None:
self.logger.critical('self.lite is None, shutting down...')
sys.exit(1)
else:
con = self.lite.getConnection(filename)
return con
So basically the SQLLite class handles my database connection, if
another class needs the connection I call getActiveConection, this
returns the current connection if there is no connection then it creates
one, if there is a connection then it returns the connection.
The other thing you can do, is create your class in the other module and
have methods that pass the variable that you need or the current
connection to your class. Something like
import MyClass
myclass = MyClass()
myclass.setDBConnection(con)
myclass.setDBName(name)
def setDBConnection(self, con):
self.con = con
def setDBName(self, name):
self.name = name
The in your class you can do:
if self.con is None:
self.con = sqlite.connect(self.name)
mycur = self.con.cursor()
else:
mycur = self.con
Or some such thing, I hope this helps.
This is similar to the way java uses Beans, and I have been doing a lot
of Java programming lately :( I want to get back to Python soooooon.
Thanks,
Greg Givler
Information Technology
Lockwood®
10 Valley Stream Parkway
Malvern, PA 19355
Phone: (484) 605-4826
Email: [email protected]
Rich Shepard
<rshepard@appl-ecosys To: "For users of the pysqlite database module." <pysqlite-IAPFreCvJWPBWskQ1e/[email protected]>
.com> cc:
Sent by: Subject: Re: [pysqlite] Please Explain This Error
pysqlite-bounces@list
s.initd.org
11/03/2006 09:52 AM
Please respond to
"For users of the
pysqlite database
module."
On Fri, 3 Nov 2006, [email protected] wrote:
> The easiest way to do something like this is to have a class that you
> create, then reference the variable in the class as in:
>
> class foo:
> myvariable = 12
>
> from foo import myvariable
> class why:
> def printmyvariable(self):
> print myvariable
Greg,
This is the circular trap I'm in. I suppose that I did not clearly
explain
the situation so I'll try again using your example above.
Class foo has all the wxPython UI code and the application's
top-level
window. In there, on one notebook page, is a wx.TextCtrl() widget in
which
the user enters the new or existing project name. That's assigned to the
variable, 'projname.' There are other bound methods in foo that require
access to the sqlite3 project database for writing and retrieving.
Therefore, foo imports class why because that's where the database
open/create/close functions are now located.
Class why has the openDB() and closeDB() functions. They need the
'projname' variable to reference the file they are to open, create, or
close. I cannot import projname from foo because foo imports class why.
In C, I'd define projname in class foo, then declare it 'extern
projname'
in class why. I don't know the python equivalent of this.
Is this any more clear now?
Thanks very much,
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
_______________________________________________
pysqlite mailing list
pysqlite-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/pysqlite
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please call the help desk at ext 4850
______________________________________________________________________
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________