Skip to content

Commit

Permalink
Fill Category List from SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
xxd committed Apr 11, 2012
1 parent 692393d commit 4431139
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
1 change: 0 additions & 1 deletion Dandan/AppDelegate.m
Expand Up @@ -189,7 +189,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

// 创建SQLite数据库
[self createEditableCopyofDatabaseIfNeeded];

// 同步表
[self initializeDatabase];

Expand Down
2 changes: 2 additions & 0 deletions Dandan/LocalSQLiteOperate.h
Expand Up @@ -13,6 +13,8 @@
}
- (NSString *) getDBPath;
- (NSInteger)CreateNewList:(sqlite3 *)db listID:(NSNumber *)listID listTitle:(NSString *)listTitle categoryID:(NSNumber *)categoryID;
- (NSArray *)getCategoryList;
-(NSInteger)insertNewTodoIntoDatabase;


@end
47 changes: 27 additions & 20 deletions Dandan/LocalSQLiteOperate.m
Expand Up @@ -7,16 +7,19 @@
//

#import "LocalSQLiteOperate.h"
#import "PickCategoryTableViewController.h"
static sqlite3_stmt *insert_statement = nil;
static sqlite3_stmt *selectstmt = nil;

@implementation LocalSQLiteOperate

//获取数据库路径
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"dan.sqlite"];
}

//建立新列表list
- (NSInteger)CreateNewList:(sqlite3 *)db listID:(NSNumber *)listID listTitle:(NSString *)listTitle categoryID:(NSNumber *)categoryID{
if (sqlite3_open([self.getDBPath UTF8String], &db) == SQLITE_OK) {
NSLog(@"path:%@",self.getDBPath);
Expand All @@ -42,28 +45,32 @@ - (NSInteger)CreateNewList:(sqlite3 *)db listID:(NSNumber *)listID listTitle:(N
}
}


- (NSInteger)insertNewTodoIntoDatabase {

if (insert_statement == nil) {
//database = db;
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO `lists` (`id`, `title`, `category_id`) VALUES(7,'test7',7)"];
const char *sql = [insertSQL UTF8String];
NSLog(@"Insert SQL: %s",sql);
if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
//获取Category列表
- (NSArray *)getCategoryList{
if (sqlite3_open([[self getDBPath] UTF8String], &database) == SQLITE_OK) {
// Get the primary key for all books.
const char *sql = "SELECT name FROM categories";
NSMutableArray *categroyNameArray = [[NSMutableArray alloc] init];
PickCategoryTableViewController *pickCategoryTableViewController = [[PickCategoryTableViewController alloc] init];
if (sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while (sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
[categroyNameArray addObject:name];


}
return categroyNameArray;
}
sqlite3_finalize(selectstmt);
} else {
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
}
int success = sqlite3_step(insert_statement);

sqlite3_reset(insert_statement);
if (success != SQLITE_ERROR) {
return sqlite3_last_insert_rowid(database);
}
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
return -1;

//调试用 Debug Only!
- (NSInteger)insertNewTodoIntoDatabase {
NSLog(@"as good as it gets!");
}


@end
7 changes: 3 additions & 4 deletions Dandan/PickCategoryTableViewController.h
Expand Up @@ -7,16 +7,15 @@
//

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

@class PickCategoryTableViewController;
@protocol pickCategoryTableViewControllerDelegate <NSObject>
-(void)controller:(PickCategoryTableViewController *)controller didSelectCategory:(NSString *)category;
@end

@interface PickCategoryTableViewController : UITableViewController{
sqlite3 *database;
}
@interface PickCategoryTableViewController : UITableViewController
@property (strong, nonatomic) NSArray *categories;
@property (nonatomic, weak) id <pickCategoryTableViewControllerDelegate> delegate;

- (void)fillCategoryList;
@end
18 changes: 10 additions & 8 deletions Dandan/PickCategoryTableViewController.m
Expand Up @@ -7,6 +7,7 @@
//

#import "PickCategoryTableViewController.h"
#import "LocalSQLiteOperate.h"

@interface PickCategoryTableViewController ()

Expand All @@ -15,19 +16,19 @@ @interface PickCategoryTableViewController ()
@implementation PickCategoryTableViewController
@synthesize delegate, categories;

- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"dan.sqlite"];
- (void)fillCategoryList{
if ([[LocalSQLiteOperate alloc] respondsToSelector:@selector(getCategoryList)])
{
categories = [[LocalSQLiteOperate alloc] performSelector:@selector(getCategoryList)];
} else {
NSLog(@"## Class does not respond to getCategoryList");
}
}

- (void)InitCategoryList{

}
- (void)viewDidLoad
{
[super viewDidLoad];
categories = [NSArray arrayWithObjects:@"Fashion", @"Tech", @"Travel", nil];
[self fillCategoryList];
}

- (void)viewDidUnload
Expand All @@ -50,6 +51,7 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [categories count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Expand Down

0 comments on commit 4431139

Please sign in to comment.