Re: Using fmdb with Cocoa Bindings
Re: Using fmdb with Cocoa Bindings
- Subject: Re: Using fmdb with Cocoa Bindings
- From: Kelvin Chung <email@hidden>
- Date: Wed, 27 May 2009 14:14:43 -0600
On May 27, 2009, at 11:06 AM, Bill Garrison wrote:
On May 27, 2009, at 1:33 AM, Kelvin Chung wrote:
I'm fairly new to both fmdb and Cocoa bindings, so I am wondering
if anyone can help me out:
Suppose I have an FMResultSet resulting in a query. I'm trying to
put the results in an NSTableView with Cocoa Bindings. Now the
table view has its contentArray bound to an NSArrayController (call
it controller), whose content array is bound to the key "species"
of a class named "SpeciesView".
So, in SpeciesView I should have -countOfSpecies and -
objectInSpeciesAtIndex:. However, I'm now having trouble bridging
the two ends, and I am wondering if someone experienced can help:
how can I get from an FMResultSet the implementations of these two
methods?
With an FMResultSet, you need to process it further to extract the
juicy Cocoa objects. A result set will have 0, 1, or n rows of data
in it, depending on the nature of the SQL query you executed.
Here's a snippet derived from the fmdb.m test example:
FMResultSet *rs = [db executeQuery:@"select * from test where a
= ?", @"hi"];
while ( [rs next] ) {
// just print out what we've got in a number of formats.
NSLog(@"%d %@ %@ %@ %f %f",
[rs intForColumn:@"c"],
[rs stringForColumn:@"b"],
[rs stringForColumn:@"a"],
[rs dateForColumn:@"d"],
[rs doubleForColumn:@"d"],
[rs doubleForColumn:@"e"]);
}
This SQL query asks for all columns in the 'test' table where column
'a' equals "hi". The result set can contain 0..n rows, so you use a
while loop to iterate the result set, accessing the objects out of
each column as needed. You'll need a similar while loop to process
your result set, populating the 'species' array in your model.
Right. Having done so, I'm puzzled as to where the code to do so
actually goes:
Suppose I have an application delegate class that creates the
FMDatabase, and I have a query that populates the table view. Suppose
the controller code is in SpeciesView, where I have a query string.
Right now, I have it -species execute the query to populate the table
(prolly a bad idea, but I don't know how to do it better).
However, it seems that the -species is executed before the database is
open (in particular, -applicationDidFinishLaunching: in the
application delegate, which loads the database). Thus, the table view
appears empty. How can I change this so that the table view is
initially populated?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden