将大型sqlite数据库导入tableview并进行更新

时间:2015-08-25 14:39:51

标签: ios objective-c xcode sqlite uitableview

注意:我对C或IOS开发知之甚少。

我有一个小的sqlite数据库(110Kb),有大约700条记录。我想知道将它们导入Tableview的最佳方法。 此外,当我单击一个单元格时,应用程序将移动到另一个ViewController,并使用Tableview中的数据更新数据库文件,具体取决于是使用登录还是退出按钮。 到目前为止,我已经找到了这个网站(我刚刚下载了这个项目)并使用它来尝试学习一些东西: http://klanguedoc.hubpages.com/hub/IOS-5-How-To-Display-SQLite-Data-in-a-UITableView
任何帮助将不胜感激:)代码为.m文件插入记录到tableview:

//  AuthorVC.m


#import "AuthorVC.h"
#import "Author.h"
#import <sqlite3.h>


@implementation AuthorVC
@synthesize theauthors;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [self authorList];
    [super viewDidLoad];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

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

    // Return the number of rows in the section.
    return [self.theauthors count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AuthorsCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    int rowCount = indexPath.row;

    Author *author = [self.theauthors objectAtIndex:rowCount];
    cell.textLabel.text = author.name;
    cell.detailTextLabel.text = author.title;


    return cell;
}


-(NSMutableArray *) authorList{
    theauthors = [[NSMutableArray alloc] initWithCapacity:10];
    @try {
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"AuthorsDb.sqlite"];
        BOOL success = [fileMgr fileExistsAtPath:dbPath];
        if(!success)
        {
            NSLog(@"Cannot locate database file '%@'.", dbPath);
        }
        if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
        {
            NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

        }


        const char *sql = "SELECT * FROM  books";
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
            NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
        }else{

            while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
                Author * author = [[Author alloc] init];
                author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
                author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
                author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
                [theauthors addObject:author];
            }
        }
        sqlite3_finalize(sqlStatement);

    }
    @catch (NSException *exception) {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }
    @finally {
        sqlite3_close(db);

        return theauthors;
    }
}
@end

1 个答案:

答案 0 :(得分:-1)

说实话,这是一项相当多的工作,而不是在Stack Overflow答案中很容易实现的。有两个地方你应该看看类似于你的要求的例子。

Apple CoreData recipes 这非常接近您的需求。阅读并了解他们提供的项目,以了解所需的内容和涉及的工作量。

此外: Ray Wenderlich CoreData Getting Started 这将为您提供更温和的CoreData介绍以及如何从一个视图转换到另一个视图。本教程是一步一步的解释,适合任何级别的经验。

Ray还提供了有关如何与服务器通信的精彩教程,您可以通过api调用了解如何更新数据库。

希望这会有所帮助。你必须深入了解并亲自动手学习。