-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteTestSecondViewController.m
More file actions
127 lines (98 loc) · 3.22 KB
/
SQLiteTestSecondViewController.m
File metadata and controls
127 lines (98 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// SQLiteTestSecondViewController.m
// SQLiteTest
//
// Created by Yingyi Dai on 12-10-25.
// Copyright (c) 2012年 SCUT. All rights reserved.
//
#import "SQLiteTestSecondViewController.h"
#import <sqlite3.h>
@interface SQLiteTestSecondViewController ()
@end
@implementation SQLiteTestSecondViewController
@synthesize scoreArray;
@synthesize table;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"View", @"View");
self.tabBarItem.image = [UIImage imageNamed:@"second"];
scoreArray = [[NSMutableArray alloc]init];
}
return self;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}
- (void)viewDidLoad
{
scoreArray = [[NSMutableArray alloc]init];
[self queryDB];
NSLog(@"hahah");
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.//
}
- (void)viewDidUnload
{
[table release];
[self setTable:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:kFilename];
}
- (void)queryDB
{
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
}
char *select = "SELECT SCORE FROM SCORE ORDER BY SCORE DESC;";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, select, -1, &stmt, nil) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW)
{
char *score = (char*)sqlite3_column_text(stmt, 0);
NSString *scoreString = [[NSString alloc] initWithUTF8String:score];
[scoreArray addObject:scoreString];
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [scoreArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"ScoreTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [scoreArray objectAtIndex:row];
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[scoreArray removeAllObjects];
[self queryDB];
[table reloadData];
}
- (void)dealloc {
[table release];
[super dealloc];
}
@end