如何在每次启动iPhone应用程序时加载SQLITE数据库(Mydb.db)?

时间:2011-03-11 22:21:00

标签: ios4

我有一个应用程序,可以在运行时访问静态SQLITE数据库。不允许用户或应用程序更改或更新此数据库。但是,我需要添加记录或更改字段内容以跟上更改URL等。我使用MesaSQLite更新单个表。但是当我在Xcode Simulator中重新打开应用程序时,没有任何更改生效。就像应用程序没有重新加载更新的数据库一样。

如何进行重装?请记住,我仍然处于收缩包装中,因为我对编码很新!

2 个答案:

答案 0 :(得分:0)

我不太确定SQL部分。但我认为您需要在从数据库获取更新后重新加载表内容。如果您发布一些代码,我可以帮助您更好。以下是重新加载表的方法:

[myTable reloadData];

您需要使用

中的新数据更新表格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

答案 1 :(得分:0)

这是一款IOS4.2 iPhone应用程序。以下是访问和显示数据库排序结果的代码。目的是使用数据库按事件和状态对委托进行排序,然后显示结果。显示的代表链接到一个网站。同样,用户和应用程序不会改变数据库,它是静态的。我使用SQLite编辑器进行更改并将它们导回到数据库的单个表中。我只需要在代码的这部分运行时重新加载表。

我需要知道代码和在何处插入表重新加载。

感谢您的帮助!!!!!

//

// delegateList.m

// foundingFathers

//

//于6/23/10由 __ 创建。

//版权所有2010 MyCompanyName 。保留所有权利。

//

import“delegateList.h”

static sqlite3 * database = nil;

@implementation delegateList

@synthesize tableView;

- (无效)viewDidLoad中

{

nameArray = [[NSMutableArray alloc] init];

stateArray = [[NSMutableArray alloc] init];

yearsArray = [[NSMutableArray alloc] init];

contArray = [[NSMutableArray alloc] init];

indArray = [[NSMutableArray alloc] init];

confArray = [[NSMutableArray alloc] init];

constArray = [[NSMutableArray alloc] init]; 

urlArray = [[NSMutableArray alloc] init];



stateNameArray = [[NSMutableArray alloc] init];

eventNameArray = [[NSMutableArray alloc] init];



[self loadStateData];

[self loadEventData];







// Setup some globals

databaseName = @"mydb.db";



// Get the path to the documents directory and append the databaseName

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir = [documentPaths objectAtIndex:0];

databasePath = [documentsDir stringByAppendingPathComponent:databaseName];



// Execute the "checkAndCreateDatabase" function

[self checkAndCreateDatabase];



// Query the database for all delegates

[self loadDataWithState:@"" andEvent:0];

}

- (无效)loadStateData

{

[stateNameArray addObject:@"All States"];

[stateNameArray addObject:@"Connecticut"];

[stateNameArray addObject:@"Delaware"];

[stateNameArray addObject:@"Georgia"];

[stateNameArray addObject:@"Maryland"];

[stateNameArray addObject:@"Massachusetts"];

[stateNameArray addObject:@"New Hampshire"];

[stateNameArray addObject:@"New Jersey"];

[stateNameArray addObject:@"New York"];

[stateNameArray addObject:@"North Carolina"];

[stateNameArray addObject:@"Pennsylvania"];

[stateNameArray addObject:@"Rhode Island"];

[stateNameArray addObject:@"South Carolina"];

[stateNameArray addObject:@"Virginia"];

}

- (无效)loadEventData

{

[eventNameArray addObject:@"All Events"];

[eventNameArray addObject:@"Continental Association"];

[eventNameArray addObject:@"Declaration of Independence"];

[eventNameArray addObject:@"Confederation of States"];

[eventNameArray addObject:@"US Constitution"];

}

- (IBAction为)nextPressed:(ID)发送方

{

[self.navigationController pushViewController:nondelegateList animated:YES];

}

- (IBAction为)menuPressed:(ID)发送方

{

[self.navigationController popToViewController:mainmenu animated:YES];

}

/ *

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

switch (interfaceOrientation) {

    case UIInterfaceOrientationPortrait:

    case UIInterfaceOrientationPortraitUpsideDown:

        return NO;

        break;

    default:

        return YES;

        break;

}

} * /

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    返回[nameArray count] +1;

}

- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

MyIdentifier = @"tblViewCell";

TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if(cell == nil) {

    [[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];

    cell = tblCell;

}



UILabel* lbl;

if (indexPath.row == 0) //table Headers

{



    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];



    lbl = (UILabel *)[cell viewWithTag:1];

    [lbl setFont:[UIFont boldSystemFontOfSize:14]];

    lbl.text = @"Name";

    [lbl setTextAlignment:UITextAlignmentLeft];



    lbl = (UILabel *)[cell viewWithTag:2];

    [lbl setFont:[UIFont boldSystemFontOfSize:14]];

    lbl.text = @"State";

    [lbl setTextAlignment:UITextAlignmentLeft];



} else {

    //[lbl setFont:[[lbl font] fontWithSize:9]];

    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];





    lbl = (UILabel *)[cell viewWithTag:1];

    [lbl setTextAlignment:UITextAlignmentLeft];

    [lbl setFont:[UIFont systemFontOfSize:14]];

    lbl.text = [nameArray objectAtIndex:indexPath.row-1];



    lbl = (UILabel *)[cell viewWithTag:2];

    [lbl setTextAlignment:UITextAlignmentLeft];

    [lbl setFont:[UIFont systemFontOfSize:14]];

    lbl.text = [stateArray objectAtIndex:indexPath.row-1];



}       



return cell;

}

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.row == 0) {

    return;

}

NSURL* page = [NSURL URLWithString:[urlArray objectAtIndex:indexPath.row - 1] ];

NSURLRequest* pageRequest = [NSURLRequest requestWithURL:page];

[webTitle setTitle:[nameArray objectAtIndex:indexPath.row - 1]];

[web loadRequest:pageRequest];

[self.navigationController pushViewController:webView animated:YES];

}

- (void)checkAndCreateDatabase {

BOOL success;



NSFileManager *fileManager = [NSFileManager defaultManager];



success = [fileManager fileExistsAtPath:databasePath];



if(success) return;



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



[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];



//[fileManager release];

}

- (void)loadDataWithState:(NSString *)stateSearch andEvent:(int)eventSearch

{

BOOL hasEvent = (BOOL)eventSearch;

BOOL hasState = !([stateSearch isEqualToString:@"All States"] || stateSearch.length <=0);

NSString* state = stateSearch;

NSString* event = nil;

switch (eventSearch) {

    case 1:

        event = @"continental";

        break;

    case 2:

        event = @"declaration";

        break;

    case 3:

        event = @"confederation";

        break;

    case 4:

        event = @"constitution";

        break;

    default:

        break;

}









// Open the database from the users filessytem





if(database || sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    // Setup the SQL Statement and compile it for faster access

    NSString* sqlString = nil;

    const char *sqlStatement;

    if (hasEvent && hasState) {

        sqlString = [[[[@"select * from founding_fathers where "stringByAppendingString:event] stringByAppendingString:@" like 'X%%' and State like '"] stringByAppendingString:state] stringByAppendingString:@"'"];

    }

    if (hasEvent && !hasState) {

        sqlString = [[@"select * from founding_fathers where "  stringByAppendingString:event] stringByAppendingString:@" like 'X%%'"];

    }

    if (!hasEvent && hasState) {

        sqlString = [[@"select * from founding_fathers where state = '" stringByAppendingString:state] stringByAppendingString:@"'"];



    }

    if (!hasEvent && !hasState) {

        sqlString= @"select * from founding_fathers";

    }



    unsigned int lengthOfMessage = [sqlString length];

    char temp[lengthOfMessage + 1];

    strcpy(temp, [sqlString cString]);

    sqlStatement = temp;





    sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

        // Loop through the results and add them to the feeds array

        [nameArray removeAllObjects];

        [stateArray removeAllObjects];

        [yearsArray removeAllObjects];

        [contArray removeAllObjects];

        [indArray removeAllObjects];

        [confArray removeAllObjects];

        [constArray removeAllObjects];

        [urlArray removeAllObjects];



        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

            // Read the data from the result row

            NSString* dName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

            NSString* dState = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

            NSString* dYears = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

            NSString* dCont = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

            NSString* dInd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];

            NSString* dConf = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];

            NSString* dConst = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];

            NSString* dUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];





            [nameArray addObject:dName];

            [stateArray addObject:dState];

            [yearsArray addObject:dYears];

            [contArray addObject:dCont];

            [indArray addObject:dInd];

            [confArray addObject:dConf];

            [constArray addObject:dConst];

            [urlArray addObject:dUrl];



        }

    }

    // Release the compiled statement from memory

    sqlite3_finalize(compiledStatement);



}

}

- (无效)viewDidUnload

{

sqlite3_close(database);

}

- (IBAction为)stateSearch:(ID)发送方

{

pickerArray = stateNameArray;

[picker reloadAllComponents];

[picker setHidden:NO];

[doneButton setHidden:NO];

}

- (IBAction为)eventSearch:(ID)发送方

{

pickerArray = eventNameArray;

[picker reloadAllComponents];

[picker setHidden:NO];

[doneButton setHidden:NO];

}

  • (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return [pickerArray count];

}

  • (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 1;

}

  • (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return [pickerArray objectAtIndex:row];

}

  • (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

}

  • (void)doneClicked:(id)sender

{

if (pickerArray == stateNameArray)

{

    stateSearchChoice = [stateNameArray objectAtIndex:[picker selectedRowInComponent:0]];

    [stateButton setTitle:stateSearchChoice];

}



if (pickerArray == eventNameArray) {

    eventSearchChoice = [picker selectedRowInComponent:0];

    [eventButton setTitle:[eventNameArray objectAtIndex:[picker selectedRowInComponent:0]]];

}

[picker setHidden:YES];

[doneButton setHidden:YES];



[self loadDataWithState:stateSearchChoice andEvent:eventSearchChoice];

[tableView reloadData];

}

@end