如何将 SQlite 中的数据显示到 iPhone 应用程序的表视图中

2024-02-28

我正在 Xcode 4.3 中使用 SQlite3 开发 iPhone 项目,SQlite 和 Xcode 之间的连接已完成,现在我想将我的数据显示到表视图(三个视图)中,并且它是只读的! 所以我有主表视图,选择原始 - >进入第二个视图并从数据库加载其他数据选择原始 - >进入详细信息视图以显示长文本和图像!

任何帮助表示赞赏。

AppDelegate.h

#import "AppDelegate.h"

#import "MasterViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize navigationController = _navigationController;

- (void)dealloc
{
    [_window release];
    [_navigationController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"cities.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL success = [fileManager fileExistsAtPath:dbPath];

    if (success) {

        NSLog(@"we have the database");

    } else {

        NSLog(@"we have no database");

        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"cities.sqlite"];


        BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];

        if (moved) {
            NSLog(@"database copied");
        }

    }


    MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
    self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

MasterViewController.h

#import <UIKit/UIKit.h>
#import <sqlite3.h> 


@class DetailViewController;

@interface MasterViewController : UITableViewController {
    NSMutableArray *cities;
}

@property (strong, nonatomic) DetailViewController *detailViewController;

@end

MasterViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    students = [[NSMutableArray alloc] init];
    countries = [[NSMutableArray alloc] init];

    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)] autorelease];
    self.navigationItem.rightBarButtonItem = addButton;


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"cities.sqlite"];


    sqlite3 *database;

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

        const char *sqlStatement = "select * from cities_info";

        sqlite3_stmt *compileStatement;

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


            while (sqlite3_step(compileStatement) == SQLITE_ROW) {

                NSLog(@"one record");

                NSString *cityName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compileStatement, 1)];

                [cities addObject:cityName];

            }

            NSLog(@"cities: %@",cities);  

        }


    } else {


        NSLog(@"error in database");

    }
}

块引用


我建议在 SQLite 上使用一个轻量级包装器 - 请参阅https://github.com/JohnGoodstadt/EasySQLite https://github.com/JohnGoodstadt/EasySQLite

这将允许:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _personTable.rows.count;
}

AND

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

     NSArray* row= _personTable.rows[indexPath.row];
     cell.textLabel.text = row[[_personTable colIndex:@"lastname"]];
     ...

通过使用代表 SQL 表的 iVar 进行设置:

self.personTable = [_db  ExecuteQuery:@"SELECT firstname , lastname , age , salary FROM person"];

数据库连接 iVar 传入您的 SQL 文件名:

self.db = [DBController sharedDatabaseController:@"DataTable.sqlite"];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 SQlite 中的数据显示到 iPhone 应用程序的表视图中 的相关文章

随机推荐