FMDB是一个对sqlite包装的一个类,方便简单使用
FMDB下载地址:
在项目里新建fmdb目录,然后把fmdb.xcodeproj拖进去
点击项目,添加依赖"FMDB",添加库"libsqlite3.dylib" "libFMDB.a"
修改FMDB为IOS项目
测试代码:
-
- (void)viewDidLoad
-
{
-
[super viewDidLoad];
-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
-
NSString*documentsDirectory = [[paths objectAtIndex:0] copy];
-
-
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"dss.db"];
-
FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
-
[documentsDirectory release];
-
if (![db open]) {
-
[db release];
-
return;
-
}
-
[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'", // I put in a ', and I'm not escaping
-
[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"]);
-
-
-
if (!([[rs columnNameForIndex:0] isEqualToString:@"rowid"] &&
-
[[rs columnNameForIndex:1] isEqualToString:@"a"])
-
) {
-
NSLog(@"WHOA THERE BUDDY, columnNameForIndex ISN'T WORKING!");
-
return ;
-
}
-
}
-
// 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];
-
-
[db close];
-
-
-
// Do any additional setup after loading the view, typically from a nib.
-
}
阅读(10906) | 评论(0) | 转发(0) |