使用fmdb进行SQLlite 操作

来源:互联网 发布:ubuntu smplayer 安装 编辑:程序博客网 时间:2024/05/22 01:01

ARC还是手工管理内存

在你的cocoa项目中,可以任意地使用,fmd在编译选项中判断作出正确的选择

Usage

There are three main classes in FMDB:

  1. FMDatabase - Represents a single SQLite database. Used for executing SQL statements.
  2. FMResultSet - Represents the results of executing a query on an FMDatabase.
  3. FMDatabaseQueue - If you're wanting to perform queries and updates on multiple threads, you'll want to use this class. It's described in the "Thread Safety" section below.

使用方面很简单,三个东西FMDatabase从数据库层考虑,可以作为SQL command的执行体;Resultset是执行结果集合,Queue,表明多线程执行使用;数据库的创建也可以以三个方式,第一文件,第二就是临时文件,最后一个是内存


Database Creation

An FMDatabase is created with a path to a SQLite database file. This path can be one of these three:

  1. A file system path. The file does not have to exist on disk. If it does not exist, it is created for you.
  2. An empty string (@""). An empty database is created at a temporary location. This database is deleted with the FMDatabaseconnection is closed.
  3. NULL. An in-memory database is created. This database will be destroyed with the FMDatabase connection is closed.
涉及到具体操作,例如执行更新(非Select),包括CREATEPRAGMAUPDATE,INSERTALTERCOMMITBEGINDETACHDELETEDROPENDEXPLAINVACUUM

这些函数的返回是一个BOOL,如果是NO。检查更多NSError的信息

对于Select,就需要用Resultset去接收返回的records:

FMResultSet *s = [db executeQuery:@"SELECT * FROM myTable"];while ([s next]) {    //retrieve values for each record}
语法看起来也很简单,遍历,然后获取每行中各字段的值(有一大堆方法/体力活):

  • intForColumn:
  • longForColumn:
  • longLongIntForColumn:
  • boolForColumn:
  • doubleForColumn:
  • stringForColumn:
  • dateForColumn:
  • dataForColumn:
  • dataNoCopyForColumn:
  • UTF8StringForColumnIndex:
  • objectForColumn:
使用示范代码:

- (IBAction)doButton:(id)sender {    NSFileManager* fm = [[NSFileManager alloc] init];    NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    NSString* dbpath = [docsdir stringByAppendingPathComponent:@"people.db"];        [fm removeItemAtPath:dbpath error:nil]; // in case we already did this once        FMDatabase* db = [FMDatabase databaseWithPath:dbpath];    if (![db open]) {        NSLog(@"Ooops");        return;    }    [db executeUpdate:@"create table people (lastname text, firstname text)"];        [db beginTransaction];    [db executeUpdate:@"insert into people (firstname, lastname) values ('Matt', 'Neuburg')"];    [db executeUpdate:@"insert into people (firstname, lastname) values ('Snidely', 'Whiplash')"];    [db executeUpdate:@"insert into people (firstname, lastname) values ('Dudley', 'Doright')"];    [db commit];        NSLog(@"I think I created it");    [db close];}- (IBAction)doButton2:(id)sender {    NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    NSString* dbpath = [docsdir stringByAppendingPathComponent:@"people.db"];        FMDatabase* db = [FMDatabase databaseWithPath:dbpath];    if (![db open]) {        NSLog(@"Ooops");        return;    }        FMResultSet *rs = [db executeQuery:@"select * from people"];    while ([rs next]) {        NSLog(@"%@ %@", [rs stringForColumn:@"firstname"], [rs stringForColumn:@"lastname"]);    }        [db close];}



感谢This is an Objective-C wrapper around SQLite: http://sqlite.org/:

https://github.com/ccgus/fmdb

原创粉丝点击