当前位置: 代码迷 >> SQL >> sqlite的容易示例
  详细解决方案

sqlite的容易示例

热度:176   发布时间:2016-05-05 15:20:52.0
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
  相关解决方案