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.");
18 // create a bad statement, just to test the error code.
19 [db executeUpdate:@"blah blah blah"];
21 NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
24 // but of course, I don't bother checking the error codes below.
25 // Bad programmer, no cookie.
27 [db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)"];
30 [db beginTransaction];
33 [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
34 @"hi'", // look! I put in a ', and I'm not escaping it!
35 [NSString stringWithFormat:@"number %d", i],
36 [NSNumber numberWithInt:i],
38 [NSNumber numberWithFloat:2.2f]];
43 FMResultSet *rs = [db executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
45 // just print out what we've got in a number of formats.
46 NSLog(@"%d %@ %@ %@ %@ %f %f",
47 [rs intForColumn:@"c"],
48 [rs stringForColumn:@"b"],
49 [rs stringForColumn:@"a"],
50 [rs stringForColumn:@"rowid"],
51 [rs dateForColumn:@"d"],
52 [rs doubleForColumn:@"d"],
53 [rs doubleForColumn:@"e"]);
55 // close the result set.
56 // it'll also close when it's dealloc'd, but we're closing the database before
57 // the autorelease pool closes, so sqlite will complain about it.
60 // ----------------------------------------------------------------------------------------
62 [db executeUpdate:@"create table blobTable (a text, b blob)"];
64 // let's read in an image from safari's app bundle.
65 NSData *d = [NSData dataWithContentsOfFile:@"/Applications/Safari.app/Contents/Resources/compass.icns"];
67 [db executeUpdate:@"insert into blobTable (a, b) values (?,?)", @"safari's compass", d];
69 rs = [db executeQuery:@"select b from blobTable where a = ?", @"safari's compass"];
71 d = [rs dataForColumn:@"b"];
72 [d writeToFile:@"/tmp/compass.icns" atomically:NO];
74 // let's look at our fancy image that we just wrote out..
75 system("/usr/bin/open /tmp/compass.icns");
78 NSLog(@"Could not select image.");
85 NSLog(@"Can't find compass image..");
89 // test out the convenience methods in +Additions
90 [db executeUpdate:@"create table t1 (a integer)"];
91 [db executeUpdate:@"insert into t1 values (5)"];
92 int a = [db intForQuery:@"select a from t1 where a = ?", [NSNumber numberWithInt:5]];
94 NSLog(@"intForQuery didn't work (a != 5)");
98 // test the busy rety timeout schtuff.
100 [db setBusyRetryTimeout:50000];
102 FMDatabase *newDb = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
105 rs = [newDb executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
106 [rs next]; // just grab one... which will keep the db locked.
108 NSLog(@"Testing the busy timeout");
110 BOOL success = [db executeUpdate:@"insert into t1 values (5)"];
113 NSLog(@"Whoa- the database didn't stay locked!");
117 NSLog(@"Hurray, our timeout worked");
123 success = [db executeUpdate:@"insert into t1 values (5)"];
125 NSLog(@"Whoa- the database shouldn't be locked!");
129 NSLog(@"Hurray, we can insert again!");