Will Parsons
2010-Feb-27 01:39 UTC
[fxruby-users] Accessing SQLite database in FXRuby application
I''m trying to develop an application that accesses an SQLite database and am running into problems related to using getApp (probably because of some simple misunderstanding of how FXRuby works on my part). The following is a stripped-down version of my (non-functioning) program: =======================================================================#!/usr/bin/env ruby require ''fox16'' require ''sqlite3'' include Fox class TestApp < FXApp def initialize super(''Test'', ''Test'') @db = SQLite3::Database.new(''test.db3'') @db || raise("can''t connect to database") end def db @db end end class TestTable < FXTable def initialize(p) super(p, :opts => LAYOUT_FILL|PACK_UNIFORM_WIDTH|TABLE_COL_SIZABLE) db = getApp.db db || raise("can''t access DB") # <---- this condition is raised setTableSize(3, 3) # fill table with data from database... end end class TopLevelWindow < FXMainWindow def initialize(app) super(app, ''Test'', :width => 400, :height => 100) TestTable.new(self) end def create super show(PLACEMENT_SCREEN) end end TestApp.new do |app| TopLevelWindow.new(app) app.create app.run end ======================================================================= When run, the condition "can''t access DB" is raised. This is probably simple, but I can''t seem to figure out what I''m doing wrong. -- Will
Nataraj S Narayan
2010-Feb-27 10:39 UTC
[fxruby-users] Accessing SQLite database in FXRuby application
Hi Should''nt it be like this? class TestApp < FXApp def initialize super(''Test'', ''Test'') end def db @db = SQLite3::Database.new(''test.db3'') @db || raise("can''t connect to database") end end regards Nataraj On Sat, Feb 27, 2010 at 7:09 AM, Will Parsons <wbparsons at cshore.com> wrote:> I''m trying to develop an application that accesses an SQLite database and > am running into problems related to using getApp (probably because of some > simple misunderstanding of how FXRuby works on my part). The following is > a stripped-down version of my (non-functioning) program: > > =======================================================================> #!/usr/bin/env ruby > > require ''fox16'' > require ''sqlite3'' > > include Fox > > class TestApp < FXApp > def initialize > super(''Test'', ''Test'') > @db = SQLite3::Database.new(''test.db3'') > @db || raise("can''t connect to database") > end > > def db > @db > end > end > > class TestTable < FXTable > def initialize(p) > super(p, :opts => LAYOUT_FILL|PACK_UNIFORM_WIDTH|TABLE_COL_SIZABLE) > > db = getApp.db > db || raise("can''t access DB") # <---- this condition is raised > > setTableSize(3, 3) > # fill table with data from database... > end > end > > class TopLevelWindow < FXMainWindow > def initialize(app) > super(app, ''Test'', :width => 400, :height => 100) > TestTable.new(self) > end > > def create > super > show(PLACEMENT_SCREEN) > end > end > > TestApp.new do |app| > TopLevelWindow.new(app) > app.create > app.run > end > > =======================================================================> > When run, the condition "can''t access DB" is raised. This is probably simple, > but I can''t seem to figure out what I''m doing wrong. > > -- > Will > _______________________________________________ > fxruby-users mailing list > fxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/fxruby-users >
Joey Kinsella
2010-Feb-27 14:34 UTC
[fxruby-users] Accessing SQLite database in FXRuby application
Try changing the following block: TestApp.new do |app| TopLevelWindow.new(app) app.create app.run end to: app = TestApp.new TopLevelWindow.new(app) app.create app.run I think (but I could be mistaken) when you pass a block to FXApp.new it''s more like this: if block_given? yield(FXApp.new(name, vendor)) else # .... end If this is the case, then @db would never get initialized, since the overloaded initialized method would never be called. On Fri, Feb 26, 2010 at 8:39 PM, Will Parsons <wbparsons at cshore.com> wrote:> I''m trying to develop an application that accesses an SQLite database and > am running into problems related to using getApp (probably because of some > simple misunderstanding of how FXRuby works on my part). The following is > a stripped-down version of my (non-functioning) program: > > =======================================================================> #!/usr/bin/env ruby > > require ''fox16'' > require ''sqlite3'' > > include Fox > > class TestApp < FXApp > def initialize > super(''Test'', ''Test'') > @db = SQLite3::Database.new(''test.db3'') > @db || raise("can''t connect to database") > end > > def db > @db > end > end > > class TestTable < FXTable > def initialize(p) > super(p, :opts => LAYOUT_FILL|PACK_UNIFORM_WIDTH|TABLE_COL_SIZABLE) > > db = getApp.db > db || raise("can''t access DB") # <---- this condition is raised > > setTableSize(3, 3) > # fill table with data from database... > end > end > > class TopLevelWindow < FXMainWindow > def initialize(app) > super(app, ''Test'', :width => 400, :height => 100) > TestTable.new(self) > end > > def create > super > show(PLACEMENT_SCREEN) > end > end > > TestApp.new do |app| > TopLevelWindow.new(app) > app.create > app.run > end > > =======================================================================> > When run, the condition "can''t access DB" is raised. This is probably > simple, > but I can''t seem to figure out what I''m doing wrong. > > -- > Will > _______________________________________________ > fxruby-users mailing list > fxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/fxruby-users >-- If you are not the intended recipient, you are hereby notified that any dissemination, distribution, copying or other use of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/fxruby-users/attachments/20100227/aeb8edf3/attachment.html>
Will Parsons
2010-Feb-27 15:21 UTC
[fxruby-users] Accessing SQLite database in FXRuby application
From: Nataraj S Narayan <natarajsn at gmail.com> Subject: Re: [fxruby-users] Accessing SQLite database in FXRuby application Date: Sat, 27 Feb 2010 16:09:22 +0530> Hi > > Should''nt it be like this? > > > class TestApp < FXApp > def initialize > super(''Test'', ''Test'') > > > end > > def db > @db = SQLite3::Database.new(''test.db3'') > @db || raise("can''t connect to database") > end > endI don''t think so, since that would give me a different database connexion each time I invoke getApp.db, but that gave me the idea that a slight variation might work: class TestApp < FXApp #... def db @db ||= SQLite3::Database.new(''test.db3'') @db || raise("can''t connect to database") end end It looks like that should defer opening the database until it is actually needed and keep the handle for later invocations. Still, it seems less straightforward than opening the database as part of the application initialization. Thanks for the suggestion.> > On Sat, Feb 27, 2010 at 7:09 AM, Will Parsons <wbparsons at cshore.com> wrote: > > I''m trying to develop an application that accesses an SQLite database and > > am running into problems related to using getApp (probably because of some > > simple misunderstanding of how FXRuby works on my part). The following is > > a stripped-down version of my (non-functioning) program: > > > > =======================================================================> > #!/usr/bin/env ruby > > > > require ''fox16'' > > require ''sqlite3'' > > > > include Fox > > > > class TestApp < FXApp > > def initialize > > super(''Test'', ''Test'') > > @db = SQLite3::Database.new(''test.db3'') > > @db || raise("can''t connect to database") > > end > > > > def db > > @db > > end > > end > > > > class TestTable < FXTable > > def initialize(p) > > super(p, :opts => LAYOUT_FILL|PACK_UNIFORM_WIDTH|TABLE_COL_SIZABLE) > > > > db = getApp.db > > db || raise("can''t access DB") # <---- this condition is raised > > > > setTableSize(3, 3) > > # fill table with data from database... > > end > > end > > > > class TopLevelWindow < FXMainWindow > > def initialize(app) > > super(app, ''Test'', :width => 400, :height => 100) > > TestTable.new(self) > > end > > > > def create > > super > > show(PLACEMENT_SCREEN) > > end > > end > > > > TestApp.new do |app| > > TopLevelWindow.new(app) > > app.create > > app.run > > end > > > > =======================================================================> > > > When run, the condition "can''t access DB" is raised. This is probably simple, > > but I can''t seem to figure out what I''m doing wrong.
William B. Parsons
2010-Feb-27 15:37 UTC
[fxruby-users] Accessing SQLite database in FXRuby application
On Sat, 27 Feb 2010 09:34:04 -0500 Joey Kinsella <jkinsella at ancillaryservices.com> wrote:> Try changing the following block: > > TestApp.new do |app| > TopLevelWindow.new(app) > app.create > app.run > end > > to: > > app = TestApp.new > TopLevelWindow.new(app) > app.create > app.run > > I think (but I could be mistaken) when you pass a block to FXApp.new it''s > more like this: > > if block_given? > yield(FXApp.new(name, vendor)) > else > # .... > end > > If this is the case, then @db would never get initialized, since the > overloaded initialized method would never be called.Thanks! That does indeed seem to be the case, so it looks like I have a solution. -- Will