【问题标题】:Importing large sqlite database to tableview and updating将大型sqlite数据库导入tableview并更新
【发布时间】:2015-11-19 08:05:07
【问题描述】:

注意:我对 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

【问题讨论】:

    标签: ios objective-c xcode sqlite uitableview


    【解决方案1】:

    老实说,这是一项相当多的工作,而不是在 Stack Overflow 答案中容易举例说明的事情。您应该在两个地方查看与您的要求相似的示例。

    Apple CoreData recipes 这非常接近您的需要。阅读并玩他们提供的项目,以了解需要什么以及涉及多少工作。

    还有: Ray Wenderlich CoreData Getting Started 这将为您提供对 CoreData 以及如何从一种视图转换到另一种视图的更温和的介绍。该教程是一步一步的解释,适合任何水平的经验。

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

    希望这会有所帮助。您必须潜入并亲自动手学习。

    【讨论】:

    • 谢谢你,它节省了大量的谷歌搜索:)。我要问的可能吗?
    • 是的,当然,在此处粘贴大量代码供您复制是不公平的,尤其是因为您不会真正学到任何东西。最好的学习方法是有一个扎实的方法。您知道您希望实现什么,记下所需的步骤,然后制定如何执行这些步骤。这就是我们专业人士每天都在做的事情。
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2012-07-27
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2016-07-16
    相关资源
    最近更新 更多