Reputation: 1347
I am doing a Bible application ,i have a default Bible database english DB.Sqlite,the use can download different language database within the application through INAPP,I know how to do the downloading,but my problem is when the user download the hebrew language database ,there are 10 language option buttons in the app.if the user download the hebrew database ,the hebrew button will enabled and he can tap the button and view it in a tableview,but when he tap the hebrew button the application database need to be switched according to the user wants,that is if the user tap the hindi database button if the user downloaded the hindi database it need to be viewed in the tableview,the the user tap the hebrew button it need to switch that database with the hebrew ,and load hebrew database in the tableview.I have the done the loading of english database how to reload this with the downloaded databases? my code for showing english database is in.h
#import <sqlite3.h>
#define DbName @"BibleDB.sqlite"
@interface DbHandler : NSObject {
}
+(void)createEditableCopyOfDatabaseIfNeeded;
+(NSString *) dataFilePath:(NSString *)path;
// Malayalam Version
+(int)mNumberOfChaptersInBook:(NSString *)book;
+(int)mNumberOfVerseForChapter:(NSString *)chapter andBook:(NSString *)book;
+(NSMutableArray *)mVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book;
+(NSString *)mVerseForVerseNo:(NSString *)verseNo OfChapter:(NSString *)chapterNo OfBook:(NSString *)book;
//+(NSString *)mVerseForVerseNo:(NSString *)verseNo OfChapter:(NSString *)chapterNo OfBook:(NSString *)book FromDB:(sqlite3 *)database;
// English Version
//+(int)eNumberOfChaptersInBook:(NSString *)book;
+(NSMutableArray *)eVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book;
+(NSMutableArray *)verseForSearchTag:(NSString *)searchTag;
//Hindi version
+(NSMutableArray *)hVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book;
+(int)hNumberOfChaptersInBook:(NSString *)book;
//+(NSMutableArray *)hverseForSearchTag:(NSString *)searchTag;
//Bookmarks
+(void)createBookmark:(NSString *)text :(NSString *)book :(NSString *)chapter :(NSString *)verse;
+(NSMutableArray *)getBookmarks;
+(bool)deleteBookmark:(NSString *)book :(NSString *)chapter :(NSString *)verse;
@end
.m
#pragma mark Englisg DB
+(NSMutableArray *)eVerseForChapter:(NSString *)chapterNo OfBook:(NSString *)book
{
NSMutableArray * result = [[NSMutableArray alloc] init];
sqlite3 *database;
NSString *dbpath;
dbpath = [DbHandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"SELECT [text] FROM english where book = '%@' and chapterNo = '%@'",book,chapterNo];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
[result addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]] ;
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"Sql Preparing Error");
}
sqlite3_close(database);
}
else
{
NSLog(@"Database not opening");
}
return result;
}
+(NSMutableArray *)verseForSearchTag:(NSString *)searchTag
{
NSMutableArray * result = [[NSMutableArray alloc] init];
sqlite3 *database;
NSString *dbpath;
dbpath = [DbHandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"SELECT [text],book,chapterNo,verseNumber FROM english where [text] like '%%%@%%%' limit 0,500",searchTag];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
[result addObject:[[NSMutableDictionary alloc] init]] ;
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] forKey:@"text"];
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"book"];
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"chapter"];
[[result lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)] forKey:@"verse"];
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"Sql Preparing Error");
}
sqlite3_close(database);
}
else
{
NSLog(@"Database not opening");
}
return result;
}
+(int)hNumberOfChaptersInBook:(NSString *)book
{
sqlite3 *database;
NSString *dbpath;
dbpath = [DbHandler dataFilePath:DbName];
if (sqlite3_open([dbpath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql = [NSString stringWithFormat:@"SELECT count(*) FROM hindi where book = '%@'",book];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
return [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] intValue];
}
}
else
{
NSLog(@"Sql Preparing Error");
}
}
else
{
NSLog(@"Database not opening");
}
return 0;
}
#pragma mark DB Setup Functions
+(void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DbName];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) {
NSLog(@"Database file already exist, so returning...");
return;
}
NSLog(@"CREATING A NEW COPY OF THE DATABASE...");
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DbName];
//[fileManager removeItemAtPath:writableDBPath error:nil];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
//Some serious problem...
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
+(NSString *) dataFilePath:(NSString *)path
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:path];
}
@end
and this my tableview code for displaying this db
static NSString *CellIdentifier = @"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
// self.table.tableFooterView = refreshFooterView;
//self.table.tableFooterView.userInteractionEnabled = YES;
}
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
// myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor clearColor]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:@"%@.%d ",delegate.selectedChapter, indexPath.row+1];
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:14.0];
cell.chapterAndVerse.frame=CGRectMake(33, 6, 30.0, 12.0);
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.backgroundColor = [UIColor clearColor];
}
Please help me to do this,If anyone have an idea about this please share with me. Thanks verymuch.
Upvotes: 2
Views: 290
Reputation: 7072
This question's been sitting for a while, so you may already have sorted out the issue.
You've posted quite a lot of code, but if I'm understanding the question the main thing that needs to happen is that in the tableView:cellForRowAtIndexPath: method, the app does something different depending on which language is selected.
Two approaches:
Either (1) in the database object (which is pointed at by delegate
in your code) have one method for all languages, such as `allSelectedVerse:'. Then, in the database object method, have a test for the language and return the appropriate text.
Or (2) in the tableView:cellForRowAtIndexPath: method run a test for the selected language and call different methods on the database object accordingly.
Either way, you'll need some way of storing the selected language. This could be in the database object or in a singleton used to keep track of settings or in any number of different ways.
Although (2) is less elegant, it takes less code to illustrate. Assuming the the currently selected language is stored in the database object using an NSString property and assuming that whenever the user changes language you send the tableView a reloadData
method, the following code should do the trick. (All this assuming I've understood your issue aright in the first place...)
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *CellIdentifier = @"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[self configureCellOnFirstCreation: cell]; // farm out configuration
}
if (tableView == table)
{
[self configureCell: cell]; // farm out cell configuration, so the content setting code is more prominent
cell.chapterAndVerse.text = [NSString stringWithFormat:@"%@.%d ",delegate.selectedChapter, indexPath.row+1];
if ([[delegate selectedLanguage] isEqualToString: @"Malayalam"])
{
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseMalayalam objectAtIndex:indexPath.row]];
}
else if ([[delegate selectedLanguage] isEqualToString: @"Hindi"])
{
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseHindi objectAtIndex:indexPath.row]];
}
else // default to English is no matching string found
{
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
}
}
else
{
// handle the other case, e.g. when we're doing search
}
}
Endnote I think you might get more and faster answers if you can post less code in your questions. In the above, I've farmed out configuration to other methods so the code flow that matters is more prominent.
Upvotes: 2