使用fmdb进行SQLlite 操作
来源:互联网 发布:ubuntu smplayer 安装 编辑:程序博客网 时间:2024/05/22 01:01
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 anFMDatabase
.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 theFMDatabase
connection is closed. NULL
. An in-memory database is created. This database will be destroyed with theFMDatabase
connection is closed.
CREATE
, PRAGMA
, UPDATE
,INSERT
, ALTER
, COMMIT
, BEGIN
, DETACH
, DELETE
, DROP
, END
, EXPLAIN
, 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, 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
- 使用fmdb进行SQLlite 操作
- 使用FMDB进行数据库操作
- 使用FMDB进行数据库操作
- 使用FMDB进行数据库操作
- 使用FMDB进行数据库操作
- 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- iOS 使用FMDB进行数据库操作
- 使用.NET操作SQLLITE
- iOS开发中使用FMDB的网文iOS 使用FMDB进行数据库操作
- Linux命令的学习与数学公式
- Maven学习
- Perl socket编程
- 10个小时尚未完成XNA中文字体的转换
- gcc __attribute__关键字举例之visibility
- 使用fmdb进行SQLlite 操作
- 软件工程积累——UML之用例图
- JSP BASEPATH
- android编译系统的makefile文件Android.mk写法如下
- Linux下SVN服务器搭建(CentOS+Subversion)
- 移动应用表单设计
- 持续集成实践成熟度模型
- POJ 1007 DNA Sorting
- C语言通用双向链表的实现