如何将数据从SQlite显示到表视图到iPhone应用程序 [英] How to Display data from SQlite into Table views to iPhone app

查看:97
本文介绍了如何将数据从SQlite显示到表视图到iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQlite3在Xcode 4.3中开发一个iPhone项目,SQlite和Xcode之间的连接已经完成,现在我想将我的数据显示到表视图(三个视图)并且只读它!
所以我有主表视图,选择raw - > take to 2nd view并从DB中加载其他数据select raw - >转到详细信息视图以显示长文本和图像!



任何帮助表示赞赏。


AppDelegate.h




  #importAppDelegate.h

#importMasterViewController.h

@implementation AppDelegate

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

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

- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc]] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//在应用程序启动后覆盖自定义点。


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(成功){

NSLog(@我们有数据库);

} else {

NSLog(@我们没有数据库);

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


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

如果(移动){
NSLog(@数据库复制);
}

}


MasterViewController * masterViewController = [[[MasterViewController alloc] initWithNibName:@MasterViewControllerbundle:nil] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
返回YES;
}

MasterViewController.h

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


@class DetailViewController;

@interface MasterViewController:UITableViewController {
NSMutableArray * cities;
}

@property(强,非原子)DetailViewController * detailViewController;

@end

MasterViewController.m

   - (void)viewDidLoad 
{
[super viewDidLoad];

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

//加载视图后进行任何其他设置,通常是从笔尖。
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 *数据库;

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:%@,城市);

}


} else {


NSLog(@数据库错误);

}
}




Blockquote



解决方案

我建议使用SQLite的轻量级包装 - 参见 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。行[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]; 


I'm working on an iPhone project in Xcode 4.3 with SQlite3, the connection between the SQlite and Xcode is done, now I want to display my data into a table views (three views) and its read only! so I have the main table view, select raw --> take to 2nd view and load other data from the DB select raw --> take to the details view to display long text and image!

Any help appreciated.

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");

    }
}

Blockquote

解决方案

I suggest a light wrapper over SQLite - see https://github.com/JohnGoodstadt/EasySQLite

This will allow:

- (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"]];
     ...

Set this up by using an iVar representing a SQL Table:

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

And a DB Connection iVar passing in your SQL file name:

self.db = [DBController sharedDatabaseController:@"DataTable.sqlite"];

这篇关于如何将数据从SQlite显示到表视图到iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆