1 #import <Foundation/Foundation.h>
3 #import "FMDatabaseAdditions.h"
5 int main (int argc, const char * argv[]) {
6 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
9 NSFileManager *fileManager = [NSFileManager defaultManager];
10 [fileManager removeFileAtPath:@"/tmp/tmp.db" handler:nil];
12 FMDatabase* db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
14 NSLog(@"Could not open db.");
19 // kind of experimentalish.
20 [db setShouldCacheStatements:YES];
22 // create a bad statement, just to test the error code.
23 [db executeUpdate:@"blah blah blah"];
25 NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
28 // but of course, I don't bother checking the error codes below.
29 // Bad programmer, no cookie.
31 [db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)"];
34 [db beginTransaction];
37 [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
38 @"hi'", // look! I put in a ', and I'm not escaping it!
39 [NSString stringWithFormat:@"number %d", i],
40 [NSNumber numberWithInt:i],
42 [NSNumber numberWithFloat:2.2f]];
48 // do it again, just because
49 [db beginTransaction];
52 [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
53 @"hi again'", // look! I put in a ', and I'm not escaping it!
54 [NSString stringWithFormat:@"number %d", i],
55 [NSNumber numberWithInt:i],
57 [NSNumber numberWithFloat:2.2f]];
65 FMResultSet *rs = [db executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
67 // just print out what we've got in a number of formats.
68 NSLog(@"%d %@ %@ %@ %@ %f %f",
69 [rs intForColumn:@"c"],
70 [rs stringForColumn:@"b"],
71 [rs stringForColumn:@"a"],
72 [rs stringForColumn:@"rowid"],
73 [rs dateForColumn:@"d"],
74 [rs doubleForColumn:@"d"],
75 [rs doubleForColumn:@"e"]);
77 // close the result set.
78 // it'll also close when it's dealloc'd, but we're closing the database before
79 // the autorelease pool closes, so sqlite will complain about it.
82 // ----------------------------------------------------------------------------------------
84 [db executeUpdate:@"create table blobTable (a text, b blob)"];
86 // let's read in an image from safari's app bundle.
87 NSData *d = [NSData dataWithContentsOfFile:@"/Applications/Safari.app/Contents/Resources/compass.icns"];
89 [db executeUpdate:@"insert into blobTable (a, b) values (?,?)", @"safari's compass", d];
91 rs = [db executeQuery:@"select b from blobTable where a = ?", @"safari's compass"];
93 d = [rs dataForColumn:@"b"];
94 [d writeToFile:@"/tmp/compass.icns" atomically:NO];
96 // let's look at our fancy image that we just wrote out..
97 system("/usr/bin/open /tmp/compass.icns");
100 NSLog(@"Could not select image.");
107 NSLog(@"Can't find compass image..");
111 // test out the convenience methods in +Additions
112 [db executeUpdate:@"create table t1 (a integer)"];
113 [db executeUpdate:@"insert into t1 values (?)", [NSNumber numberWithInt:5]];
114 int a = [db intForQuery:@"select a from t1 where a = ?", [NSNumber numberWithInt:5]];
116 NSLog(@"intForQuery didn't work (a != 5)");
119 // test the busy rety timeout schtuff.
121 [db setBusyRetryTimeout:50000];
123 FMDatabase *newDb = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
126 rs = [newDb executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
127 [rs next]; // just grab one... which will keep the db locked.
129 NSLog(@"Testing the busy timeout");
131 BOOL success = [db executeUpdate:@"insert into t1 values (5)"];
134 NSLog(@"Whoa- the database didn't stay locked!");
138 NSLog(@"Hurray, our timeout worked");
144 success = [db executeUpdate:@"insert into t1 values (5)"];
146 NSLog(@"Whoa- the database shouldn't be locked!");
150 NSLog(@"Hurray, we can insert again!");
155 // test some nullness.
156 [db executeUpdate:@"create table t2 (a integer, b integer)"];
158 if (![db executeUpdate:@"insert into t2 values (?, ?)", nil, [NSNumber numberWithInt:5]]) {
159 NSLog(@"UH OH, can't insert a nil value for some reason...");
165 rs = [db executeQuery:@"select * from t2"];
167 NSString *a = [rs stringForColumnIndex:0];
168 NSString *b = [rs stringForColumnIndex:1];
171 NSLog(@"%s:%d", __FUNCTION__, __LINE__);
172 NSLog(@"OH OH, PROBLEMO!");
176 NSLog(@"YAY, NULL VALUES");
179 if (![b isEqualToString:@"5"]) {
180 NSLog(@"%s:%d", __FUNCTION__, __LINE__);
181 NSLog(@"OH OH, PROBLEMO!");
195 // test some inner loop funkness.
196 [db executeUpdate:@"create table t3 (a somevalue)"];
199 // do it again, just because
200 [db beginTransaction];
203 [db executeUpdate:@"insert into t3 (a) values (?)" , [NSNumber numberWithInt:i]];
210 rs = [db executeQuery:@"select * from t3"];
212 int foo = [rs intForColumnIndex:0];
214 int newVal = foo + 100;
216 [db executeUpdate:@"update t3 set a = ? where a = ?" , [NSNumber numberWithInt:newVal], [NSNumber numberWithInt:foo]];
219 FMResultSet *rs2 = [db executeQuery:@"select a from t3 where a = ?", [NSNumber numberWithInt:newVal]];
222 if ([rs2 intForColumnIndex:0] != newVal) {
223 NSLog(@"Oh crap, our update didn't work out!");
240 [db executeUpdate:@"create table nulltest (a text, b text)"];
242 [db executeUpdate:@"insert into nulltest (a, b) values (?, ?)" , [NSNull null], @"a"];
243 [db executeUpdate:@"insert into nulltest (a, b) values (?, ?)" , nil, @"b"];
245 rs = [db executeQuery:@"select * from nulltest"];
249 NSString *a = [rs stringForColumnIndex:0];
250 NSString *b = [rs stringForColumnIndex:1];
253 NSLog(@"Oh crap, the nil / null inserts didn't work!");
258 NSLog(@"Oh crap, the nil / null inserts didn't work (son of error message)!");
262 NSLog(@"HURRAH FOR NSNULL (and nil)!");
280 // print out some stats if we are using cached statements.
281 if ([db shouldCacheStatements]) {
283 NSEnumerator *e = [[db cachedStatements] objectEnumerator];;
284 FMStatement *statement;
286 while ((statement = [e nextObject])) {
287 NSLog(@"%@", statement);