Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3687716
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: iOS平台

2013-07-29 15:17:49

FMDB是一个对sqlite包装的一个类,方便简单使用

FMDB下载地址:

https://codeload.github.com/yifangyou/fmdb/zip/master

在项目里新建fmdb目录,然后把fmdb.xcodeproj拖进去

点击项目,添加依赖"FMDB",添加库"libsqlite3.dylib" "libFMDB.a"

 

修改FMDB为IOS项目

 

测试代码:


点击(此处)折叠或打开

  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5.   
  6.   NSString*documentsDirectory = [[paths objectAtIndex:0] copy];
  7.   
  8.   NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"dss.db"];
  9.   FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
  10.   [documentsDirectory release];
  11.   if (![db open]) {
  12.     [db release];
  13.     return;
  14.   }
  15.   [db executeUpdate:@"create table test (a text, b text, c integer, d double, e double)"];
  16.   [db beginTransaction];
  17.   int i = 0;
  18.   while (i++ < 20) {
  19.     [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)" ,
  20.      @"hi'", // I put in a ', and I'm not escaping
  21.      [NSString stringWithFormat:@"number %d", i],
  22.      [NSNumber numberWithInt:i],
  23.      [NSDate date],
  24.      [NSNumber numberWithFloat:2.2f]];
  25.   }
  26.   [db commit];
  27.   
  28.   
  29.   FMResultSet *rs = [db executeQuery:@"select rowid,* from test where a = ?", @"hi'"];
  30.   while ([rs next]) {
  31.       // just print out what we've got in a number of formats.
  32.     NSLog(@"%d %@ %@ %@ %@ %f %f",
  33.           [rs intForColumn:@"c"],
  34.           [rs stringForColumn:@"b"],
  35.           [rs stringForColumn:@"a"],
  36.           [rs stringForColumn:@"rowid"],
  37.           [rs dateForColumn:@"d"],
  38.           [rs doubleForColumn:@"d"],
  39.           [rs doubleForColumn:@"e"]);
  40.     
  41.     
  42.     if (!([[rs columnNameForIndex:0] isEqualToString:@"rowid"] &&
  43.           [[rs columnNameForIndex:1] isEqualToString:@"a"])
  44.         ) {
  45.       NSLog(@"WHOA THERE BUDDY, columnNameForIndex ISN'T WORKING!");
  46.       return ;
  47.     }
  48.   }
  49.     // close the result set.
  50.     // it'll also close when it's dealloc'd, but we're closing the database before
  51.     // the autorelease pool closes, so sqlite will complain about it.
  52.   [rs close];
  53.   
  54.   [db close];
  55.  
  56.   
  57. // Do any additional setup after loading the view, typically from a nib.
  58. }


阅读(10117) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~