-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBOperation.m
182 lines (146 loc) · 6.31 KB
/
DBOperation.m
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// DBOperation.m
//
//
#import "DBOperation.h"
static sqlite3 *database = nil;
static int conn;
@implementation DBOperation
+(void)checkCreateDB{
@try {
NSString *dbPath,*databaseName;
databaseName=@"Emoji_app.rdb";
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [docPaths objectAtIndex:0];
dbPath = [docDir stringByAppendingPathComponent:databaseName];
BOOL success;
NSFileManager *fm = [NSFileManager defaultManager];
success=[fm fileExistsAtPath:dbPath];
if(success){
NSLog(@"Database copy successfully created !!");
[self OpenDatabase:dbPath];
return;
}
NSString *dbPathFromApp=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fm copyItemAtPath:dbPathFromApp toPath:dbPath error:nil];
[self OpenDatabase:dbPath];
}
@catch (NSException *exception) {
NSLog(@"%@",[exception reason]);
}
}
//Open database
+ (void) OpenDatabase:(NSString*)path
{
@try
{
conn = sqlite3_open([path UTF8String], &database);
if (conn == SQLITE_OK) {
NSLog(@"Database Open Successfully.");
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
@catch (NSException *e) {
NSLog(@"%@",e);
}
}
+(NSMutableArray*) selectData:(NSString *)sql
{
@try
{
if (conn == SQLITE_OK)
{
sqlite3_stmt *stmt = nil;
if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL) != SQLITE_OK) {
[NSException raise:@"DatabaseException" format:@"Error while creating statement. '%s'", sqlite3_errmsg(database)];
}
NSMutableArray *obj = [[NSMutableArray alloc]init];
int numResultColumns = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
numResultColumns = sqlite3_column_count(stmt);
//NSLog(@"numRecentColumns: %i",numResultColumns);
//NSMutableArray *fieldNames = [[NSMutableArray alloc] initWithCapacity:0];
@autoreleasepool {
NSMutableDictionary *tmpObj = [[NSMutableDictionary alloc]init];
for(int i = 0; i < numResultColumns; i++){
if(sqlite3_column_type(stmt, i) == SQLITE_INTEGER){
const char *name = sqlite3_column_name(stmt, i);
NSString *columnName = [[NSString alloc]initWithCString:name encoding:NSUTF8StringEncoding];
[tmpObj setObject:[NSString stringWithFormat:@"%i",sqlite3_column_int(stmt, i)] forKey:columnName];
} else if (sqlite3_column_type(stmt, i) == SQLITE_FLOAT) {
const char *name = sqlite3_column_name(stmt, i);
NSString *columnName = [[NSString alloc]initWithCString:name encoding:NSUTF8StringEncoding];
//[obj setValue:sqlite3_column_double(stmt, i) forKey:columnName];
//[tmpObj setObject:sqlite3_column_double(stmt, i) forKey:columnName];
[tmpObj setObject:[NSString stringWithFormat:@"%f",sqlite3_column_double(stmt, i)] forKey:columnName];
} else if (sqlite3_column_type(stmt, i) == SQLITE_TEXT) {
const char *name = sqlite3_column_name(stmt, i);
NSString *tmpStr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, i)];
if ( tmpStr == nil) {
tmpStr = @"";
}
NSString *columnName = [[NSString alloc]initWithCString:name encoding:NSUTF8StringEncoding];
[tmpObj setObject:tmpStr forKey:columnName];
} else if (sqlite3_column_type(stmt, i) == SQLITE_BLOB) {
//[NSString stringWithUTF8String:( *)sqlite3_column_blob(cmp_sqlStmt, i)];
// const char *name = sqlite3_column_name(stmt, i);
// NSString *columnName = [[NSString alloc]initWithCString:name encoding:NSUTF8StringEncoding];
// [obj setValue: forKey:columnName]
}
}
[obj addObject:tmpObj];
// [theResult setFieldNames:fieldNames];
}
// NSLog(@"StrengthExercise::constructResults\n====\n %@ \n======\n", sqlite3_column_text(stmt, 1));
//[res addObject:[self :stmt]];
}
return obj;
} else
return nil;
}
@catch (NSException *exception) {
NSLog(@"%@",[exception reason]);
}
}
+(BOOL) executeSQL:(NSString *)sqlTmp {
@try {
if(conn == SQLITE_OK) {
//NSLog(@"\n\n%@",sqlTmp);
const char *sqlStmt = [sqlTmp cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *cmp_sqlStmt1;
int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt1, NULL);
returnValue == SQLITE_OK ? NSLog(@"\n Inserted \n") :NSLog(@"\n Not Inserted \n");
sqlite3_step(cmp_sqlStmt1);
sqlite3_finalize(cmp_sqlStmt1);
if (returnValue == SQLITE_OK)
{
return TRUE;
}
}
return FALSE;
}
@catch (NSException *exception) {
NSLog(@"%@",[exception reason]);
}
}
+(int) getLastInsertId
{
@try {
return sqlite3_last_insert_rowid(database);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception reason]);
}
}
//Save data at application closing time
+ (void) finalizeStatements
{
@try {
if(database) sqlite3_close(database);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception reason]);
}
}
@end