学学习网 手机版

学学习网

学习路径: 学习首页 > 应用开发 > ios >

第九节 用SQLite管理数据库(3)

设置字体:
----------------------------------

    success = NO;
  }
  return success;
}
// DELETE
- (BOOL)deleteAt:(int)index{
  BOOL success = YES;
  [db executeUpdate:[self setTable:@"DELETE FROM %@ WHERE id = ?"], [NSNumber numberWithInt:index]];
  if ([db hadError]) {
    NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
    success = NO;
  }
  return success;
}
 
- (void)dealloc {
  [super dealloc];
}
 
@end
为了确认程序正确,我们添加一个UITableView。使用initWithNibName 测试DAO。
 //NoteController.h
#import <UIKit/UIKit.h>
 
@class TbNoteDao;
 
@interface NoteController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
  UITableView *myTableView;
  TbNoteDao *tbNoteDao;
  NSMutableArray *record;
}
 
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) TbNoteDao *tbNoteDao;
@property (nonatomic, retain) NSMutableArray *record;
 
@end
 
//NoteController.m
#import "NoteController.h"
#import "TbNoteDao.h"
#import "TbNote.h"
 
@implementation NoteController
@synthesize myTableView, tbNoteDao, record;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    tbNoteDao = [[TbNoteDao alloc] init];
    [tbNoteDao insertWithTitle:@"TEST TITLE" Body:@"TEST BODY"];
//    [tbNoteDao updateAt:1 Title:@"UPDATE TEST" Body:@"UPDATE BODY"];
//    [tbNoteDao deleteAt:1];
    record = [[tbNoteDao select] retain];
  }
  return self;
}
 
- (void)viewDidLoad {
  [super viewDidLoad];
  myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
  myTableView.delegate = self;
  myTableView.dataSource = self;
  self.view = myTableView;
}
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [record count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";
 
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  }
  TbNote *tr = (TbNote *)[record objectAtIndex:indexPath.row];
  cell.text = [NSString stringWithFormat:@"%i %@", [tr getIndex], tr.title];
  return cell;
}
 
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}
 
- (void)dealloc {
  [super dealloc];
}
 
@end
最后我们开看看连接DB,和添加ViewController 的处理。这一同样不使用Interface Builder。
 //SqlSampleAppDelegate.h
#import <UIKit/UIKit.h>
 
@class FMDatabase;
 
@interface SqlSampleAppDelegate : NSObject <UIApplicationDelegate> {
  UIWindow *window;
  FMDatabase *db;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) FMDatabase *db;
 
- (BOOL)initDatabase;
- (void)closeDatabase;
 
@end
 
//SqlSampleAppDelegate.m
#import "SqlSampleAppDelegate.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "NoteController.h"
 
@implementation SqlSampleAppDelegate
 
@synthesize window;
@synthesize db;
 
- (void)applicationDidFinishLaunching:(UIApplication *)application {
  if (![self initDatabase]){
    NSLog(@"Failed to init Database.");
  }
  NoteController *ctrl = [[NoteController alloc] initWithNibName:nil bundle:nil];
  [window addSubview:ctrl.view];
  [window makeKeyAndVisible];
}
 
----------------------------------
课程列表
重点难点
赞助链接