使用fmdb进行SQLlite *** 作

使用fmdb进行SQLlite  *** 作,第1张

概述ARC还是手工管理内存 在你的cocoa项目中,可以任意地使用,fmd在编译选项中判断作出正确的选择 Usage There are three main classes in FMDB: FMDatabase - Represents a single SQLite database. Used for executing SQL statements. FMResultSet - Repres

ARC还是手工管理内存

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

Usage

There are three main classes in FMDB:

FMDatabase - Represents a single sqlite database. Used for executing sql statements. FMResultSet - Represents the results of executing a query on an FMDatabase. 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:

A file system path. The file does not have to exist on disk. If it does not exist,it is created for you. An empty string (@""). An empty database is created at a temporary location. This database is deleted with the FMDatabaseconnection is closed. NulL. An in-memory database is created. This database will be destroyed with the FMDatabase connection is closed. 涉及到具体 *** 作,例如执行更新(非Select),包括 CREATEPRAGMA,248)">UPDATE,INSERT,248)">ALTER,248)">COMMIT,248)">BEGIN,248)">DETACH,248)">DELETE,248)">DROP,248)">END,248)">EXPLAIN,248)">VACUUM

这些函数的返回是一个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,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

总结

以上是内存溢出为你收集整理的使用fmdb进行SQLlite *** 作全部内容,希望文章能够帮你解决使用fmdb进行SQLlite *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1059918.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存