fmdb.m
4.35 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
128
129
130
131
132
133
134
135
136
#import <Foundation/Foundation.h>
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// delete the old db.
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:@"/tmp/tmp.db" error:NULL];
FMDatabase* db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
if (![db open]) {
NSLog(@"Could not open db.");
return 0;
}
// create a bad statement, just to test the error code.
[db executeUpdate:@"blah blah blah"];
if ([db hadError]) {
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
}
// but of course, I don't bother checking the error codes below.
// Bad programmer, no cookie.
[db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)"];
[db beginTransaction];
int i = 0;
while (i++ < 20) {
[db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
@"hi'", // look! I put in a ', and I'm not escaping it!
[NSString stringWithFormat:@"number %d", i],
[NSNumber numberWithInt:i],
[NSDate date],
[NSNumber numberWithFloat:2.2f]];
}
[db commit];
FMResultSet *rs = [db executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
while ([rs next]) {
// just print out what we've got in a number of formats.
NSLog(@"%d %@ %@ %@ %@ %f %f",
[rs intForColumn:@"c"],
[rs stringForColumn:@"b"],
[rs stringForColumn:@"a"],
[rs stringForColumn:@"rowid"],
[rs dateForColumn:@"d"],
[rs doubleForColumn:@"d"],
[rs doubleForColumn:@"e"]);
}
// close the result set.
// it'll also close when it's dealloc'd, but we're closing the database before
// the autorelease pool closes, so sqlite will complain about it.
[rs close];
// ----------------------------------------------------------------------------------------
// blob support.
[db executeUpdate:@"create table blobTable (a text, b blob)"];
// let's read in an image from safari's app bundle.
NSData *d = [NSData dataWithContentsOfFile:@"/Applications/Safari.app/Contents/Resources/compass.icns"];
if (d) {
[db executeUpdate:@"insert into blobTable (a, b) values (?,?)", @"safari's compass", d];
rs = [db executeQuery:@"select b from blobTable where a = ?", @"safari's compass"];
if ([rs next]) {
d = [rs dataForColumn:@"b"];
[d writeToFile:@"/tmp/compass.icns" atomically:NO];
// let's look at our fancy image that we just wrote out..
system("/usr/bin/open /tmp/compass.icns");
}
else {
NSLog(@"Could not select image.");
}
[rs close];
}
else {
NSLog(@"Can't find compass image..");
}
// test out the convenience methods in +Additions
[db executeUpdate:@"create table t1 (a integer)"];
[db executeUpdate:@"insert into t1 values (?)", [NSNumber numberWithUnsignedInteger:5]];
int a = [db intForQuery:@"select a from t1 where a = ?", [NSNumber numberWithUnsignedInteger:5]];
if (a != 5) {
NSLog(@"intForQuery didn't work (a != 5)");
}
// test the busy rety timeout schtuff.
[db setBusyRetryTimeout:50000];
FMDatabase *newDb = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
[newDb open];
rs = [newDb executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
[rs next]; // just grab one... which will keep the db locked.
NSLog(@"Testing the busy timeout");
BOOL success = [db executeUpdate:@"insert into t1 values (5)"];
if (success) {
NSLog(@"Whoa- the database didn't stay locked!");
return 7;
}
else {
NSLog(@"Hurray, our timeout worked");
}
[rs close];
[newDb close];
success = [db executeUpdate:@"insert into t1 values (5)"];
if (!success) {
NSLog(@"Whoa- the database shouldn't be locked!");
return 8;
}
else {
NSLog(@"Hurray, we can insert again!");
}
[db close];
[pool release];
return 0;
}