mardi 23 décembre 2014

FMDB multiple database query

Is it possible to get "Select" query from multiple database in FMDB ?


my program have a lots of database with same schema but different data and name.


user can download these databases and adding them to document folder.


now i want to get query from multiple database and save them inside Nsmutable array.


this how i handle my program with one database :


Utlity.m for accessing my main Program DB :



@implementation Utility

+(NSString *) getDatabasePath
{
NSString *databasePath = [(AppDelegate *)[[UIApplication sharedApplication] delegate] databasePath];

return databasePath;
}

+(void) showAlert:(NSString *)title message:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];

[alert show];
}


Database creation insdide appdelegate :



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[NSThread sleepForTimeInterval:2];
self.databaseName = @"News.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
self.databasePath = [documentDir stringByAppendingPathComponent:self.databaseName];
BOOL createAndCheckTables_result = [self createAndCheckTables];
NSLog(@"createAndCheckTables is : %s ", createAndCheckTables_result ? "true" : "false");
return YES;
}
-(void) createAndCheckDatabase
{
BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:self.databasePath];

if(success) return;

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseName];

[fileManager copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil];
}


how i call my main DB inside controller :



self.resultsdict = [[NSMutableArray alloc] init];
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
FMResultSet *results_test = [db executeQuery:@"SELECT * FROM News"];
while([results_test next])
{

[self.resultsdict addObject:[results_test resultDictionary]];
}


[db close];


and for one other database (Downloaded) i just change :



NSString * sql = [NSString stringWithFormat:@"SELECT * FROM news order by Id"];
NSString *pathLocal = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"44320.s3db"];
FMDatabase *db = [FMDatabase databaseWithPath:pathLocal];
[db open];
FMResultSet *results_packs = [db executeQuery:sql];
while([results_packs next])
{

[self.resultsdict addObject:[results_packs resultDictionary]];
}


i keep the list of downloaded DB name inside my main db "Db_Table" and store the s3db file inside "documentPaths". now i just want pass a list of Database to pathLocal and get result from those database inside my self.resultsdict


Aucun commentaire:

Enregistrer un commentaire