sqlite的简单示例
首先,需要将PlausibleDatabase.framework以及libsqlite3.0.dylib加入到项目中。
?
头文件:
?
#import <UIKit/UIKit.h>@class PLSqliteDatabase;@interface sqliteViewController : UIViewController {PLSqliteDatabase *db;}@property (nonatomic, retain) PLSqliteDatabase *db;- (NSArray *)getBooks;@end
?
实现文件:
?
#import "sqliteViewController.h"#import <PlausibleDatabase/PlausibleDatabase.h>@implementation sqliteViewController@synthesize db;- (id)init{if (self = [super init]) {NSString *path = [[NSBundle mainBundle] pathForResource:@"CaseDatabase" ofType:@"sqlite"];PLSqliteDatabase *_db = [[PLSqliteDatabase alloc] initWithPath:path];self.db = _db;[_db release];if (![db open])NSLog(@"Could not open database");}return self;}- (NSArray *)getBooks{NSMutableArray *books = [[NSMutableArray alloc] initWithCapacity:0];id<PLResultSet> results = [db executeQuery:@"SELECT `ID`,`TITLE`,`COVER`,`DOCTOR` FROM `BOOKS` ORDER BY`UPDATE_TIME` DESC"];while ([results next]) {NSDictionary *row = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:[results intForColumn: @"ID"]], @"ID", [results stringForColumn:@"TITLE"], @"TITLE", [results stringForColumn:@"COVER"], @"COVER", [results stringForColumn:@"DOCTOR"], @"DOCTOR", nil];[books addObject:row];NSLog(@"%@", books);[row release];}[results close];return books;}- (void)viewDidLoad { [super viewDidLoad];[self init];[self getBooks];}- (void)dealloc { [super dealloc];}@end