-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteTestFirstViewController.m
More file actions
105 lines (78 loc) · 2.62 KB
/
SQLiteTestFirstViewController.m
File metadata and controls
105 lines (78 loc) · 2.62 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
//
// SQLiteTestFirstViewController.m
// SQLiteTest
//
// Created by Yingyi Dai on 12-10-25.
// Copyright (c) 2012年 SCUT. All rights reserved.
//
#import "SQLiteTestFirstViewController.h"
#import <sqlite3.h>
@interface SQLiteTestFirstViewController ()
@end
@implementation SQLiteTestFirstViewController
@synthesize recordField1;
@synthesize submitBtn;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Input", @"Input");
self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
return self;
}
- (void)viewDidLoad
{
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
}
char* errorMsg;
NSString *createSQL = @"create table if not exists SCORE (score INTEGER);";
if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
sqlite3_close(database);
NSAssert1(0, @"error creating table :%s", errorMsg);
}
sqlite3_close(database);
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)touchBg:(id)sender
{
[recordField1 resignFirstResponder];
}
- (void)submitBtnPressed:(id)sender
{
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0,@"Failed to open database");
}
if (recordField1.text.length != 0) {
NSString *insert = [[NSString alloc] initWithFormat:@"INSERT INTO SCORE (SCORE ) VALUES ('%d');",recordField1.text.intValue];
char* errorMsg;
if (sqlite3_exec(database, [insert UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0,@"Failed to insert");
}
}
sqlite3_close(database);
}
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:kFilename];
}
@end