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");
99 // ye shall read the header for this function, or suffer the consequences.
100 d = [rs dataNoCopyForColumn:@"b"];
101 [d writeToFile:@"/tmp/compass_data_no_copy.icns" atomically:NO];
102 system("/usr/bin/open /tmp/compass_data_no_copy.icns");
105 NSLog(@"Could not select image.");
112 NSLog(@"Can't find compass image..");
116 // test out the convenience methods in +Additions
117 [db executeUpdate:@"create table t1 (a integer)"];
118 [db executeUpdate:@"insert into t1 values (?)", [NSNumber numberWithInt:5]];
119 int a = [db intForQuery:@"select a from t1 where a = ?", [NSNumber numberWithInt:5]];
121 NSLog(@"intForQuery didn't work (a != 5)");
124 // test the busy rety timeout schtuff.
126 [db setBusyRetryTimeout:50000];
128 FMDatabase *newDb = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
131 rs = [newDb executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
132 [rs next]; // just grab one... which will keep the db locked.
134 NSLog(@"Testing the busy timeout");
136 BOOL success = [db executeUpdate:@"insert into t1 values (5)"];
139 NSLog(@"Whoa- the database didn't stay locked!");
143 NSLog(@"Hurray, our timeout worked");
149 success = [db executeUpdate:@"insert into t1 values (5)"];
151 NSLog(@"Whoa- the database shouldn't be locked!");
155 NSLog(@"Hurray, we can insert again!");
160 // test some nullness.
161 [db executeUpdate:@"create table t2 (a integer, b integer)"];
163 if (![db executeUpdate:@"insert into t2 values (?, ?)", nil, [NSNumber numberWithInt:5]]) {
164 NSLog(@"UH OH, can't insert a nil value for some reason...");
170 rs = [db executeQuery:@"select * from t2"];
172 NSString *a = [rs stringForColumnIndex:0];
173 NSString *b = [rs stringForColumnIndex:1];
176 NSLog(@"%s:%d", __FUNCTION__, __LINE__);
177 NSLog(@"OH OH, PROBLEMO!");
181 NSLog(@"YAY, NULL VALUES");
184 if (![b isEqualToString:@"5"]) {
185 NSLog(@"%s:%d", __FUNCTION__, __LINE__);
186 NSLog(@"OH OH, PROBLEMO!");
200 // test some inner loop funkness.
201 [db executeUpdate:@"create table t3 (a somevalue)"];
204 // do it again, just because
205 [db beginTransaction];
208 [db executeUpdate:@"insert into t3 (a) values (?)" , [NSNumber numberWithInt:i]];
215 rs = [db executeQuery:@"select * from t3"];
217 int foo = [rs intForColumnIndex:0];
219 int newVal = foo + 100;
221 [db executeUpdate:@"update t3 set a = ? where a = ?" , [NSNumber numberWithInt:newVal], [NSNumber numberWithInt:foo]];
224 FMResultSet *rs2 = [db executeQuery:@"select a from t3 where a = ?", [NSNumber numberWithInt:newVal]];
227 if ([rs2 intForColumnIndex:0] != newVal) {
228 NSLog(@"Oh crap, our update didn't work out!");
237 [db executeUpdate:@"create table nulltest (a text, b text)"];
239 [db executeUpdate:@"insert into nulltest (a, b) values (?, ?)" , [NSNull null], @"a"];
240 [db executeUpdate:@"insert into nulltest (a, b) values (?, ?)" , nil, @"b"];
242 rs = [db executeQuery:@"select * from nulltest"];
246 NSString *a = [rs stringForColumnIndex:0];
247 NSString *b = [rs stringForColumnIndex:1];
250 NSLog(@"Oh crap, the nil / null inserts didn't work!");
255 NSLog(@"Oh crap, the nil / null inserts didn't work (son of error message)!");
259 NSLog(@"HURRAH FOR NSNULL (and nil)!");
263 // print out some stats if we are using cached statements.
264 if ([db shouldCacheStatements]) {
266 NSEnumerator *e = [[db cachedStatements] objectEnumerator];;
267 FMStatement *statement;
269 while ((statement = [e nextObject])) {
270 NSLog(@"%@", statement);
276 NSLog(@"That was version %@ of sqlite", [FMDatabase sqliteLibVersion]);