i os sqlite

前端之家收集整理的这篇文章主要介绍了i os sqlite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一,sqlite 简介

前面写了一篇博文讲如何在 C# 中使用 ADO 访问各种数据库,在移动开发和嵌入式领域也有一个轻量级的开源关系型数据库-sqlite。它的特点是零配置(无需服务器),单磁盘文件存储数据(就像fopen一样),平台无关性,使用起来简单又高效。这些特点让其非常适合移动开发和嵌入式领域。当然,sqlite 也因其力求简单高效,也就限制了它对并发,海量数据的处理。下面,我就再接再厉,来讲讲如何在 iOS 中使用 sqlite 库和第三方封装库 FMDB,以及介绍一个 MAC 下开源的可视化 sqlite 浏览器。

本文源码:https://github.com/kesalin/iOSSnippet/tree/master/SQLiteDemo

二,在 iOS 中的使用

在 iOS 中 sqlite3 库是一套纯 C 的接口,因此很方便地就可以在 obj-c 源码中无痕使用它,而且其使用方式与用 ADO 方式操作数据库大同小异-少了创建数据库链接一环而已(因为 sqlite 没有服务器的概念也就无需链接了)。

  • 首先,需要引入 libsqlite3.0.dylib:

然后包含头文件

#import"/usr/include/sqlite3.h"

  1. sqlITE_API int sqlite3_open(
  2. const char *filename,/* Database filename (UTF-8) */
  3. sqlite3 **ppDb /* OUT: sqlite db handle */
  4. );

使用示例:(dbPath 为 NSString *)

  1. // open database
  2. //
  3. int state = sqlite3_open([dbPath UTF8String],&database);
  4. if (state == sqlITE_OK) {
  5. DLOG(@" >> Succeed to open database. %@",dbPath);
  6. }
  7. else {
  8. DLOG(@" >> Failed to open database. %@"
  1. sqlITE_API int sqlite3_close(sqlite3 *);

上面这个接口将关闭数据库,如果当前还有事务没有提交,会先执行 rollback 操作,然后再关闭数据库

  • 执行 sql 语句
  1. sqlITE_API int sqlite3_exec(
  2. sqlite3*,/* An open database */
  3. const char *sql,/* sql to be evaluated */
  4. int (*callback)(void*,int,char**,char**),/* Callback function */
  5. void *,/* 1st argument to callback */
  6. char **errmsg /* Error msg written here */
  7. );

这个接口是最常用到的,几乎除了查询之外的 sql 命令都可以用它来操作,比如创建表,插入/更新/删除记录,创建/提交/回滚事务等。注意:如果 errmsg 不为 null,那么当错误发生时, sqlite 就会为错误消息分配内存,返回给调用者,调用者有责任调用 sqlite3_free 来释放这部分内存。为了方便使用,我封装了一个简单的 obj-c 方法

  1. - (BOOL)excutesqlWithCString:(const char *)sqlCmd
  2. {
  3. char * errorMsg;
  4. int state = sqlite3_exec(database,sqlCmd,NULL,0)">errorMsg);
  5. if (state == sqlITE_OK) {
  6. DLOG(@" >> Succeed to %@"sqlCmd encoding:NSUTF8StringEncoding]);
  7. }
  8. else {
  9. DLOG(@" >> Failed to %@. Error: %@"sqlCmd encoding:NSUTF8StringEncoding],[NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding]);
  10. sqlite3_free(errorMsg);
  11. }
  12. return (state == sqlITE_OK);
  13. }
下面是创建表以及事务操作的使用示例:

  1. - (void)createTable
  2. {
  3. if (database == NULL) {
  4. DLOG(@" >> Database does not open yet.");
  5. return;
  6. }
  7. const char * sqlCmd = "create table if not exists customer (id integer primary key autoincrement,name text not null,address text,age integer)";
  8. [self excutesqlWithCString:sqlCmd];
  9. }
  10. - (BOOL)beginTransaction
  11. {
  12. return [self excutesqlWithCString:"BEGIN EXCLUSIVE TRANSACTION;"];
  13. }
  14. - (BOOL)commit
  15. {
  16. return [self excutesqlWithCString:"COMMIT TRANSACTION;"];
  17. }
  18. - (BOOL)rollback
  19. {
  20. return [self excutesqlWithCString:"ROLLBACK TRANSACTION;"];
  21. }
很简单,不是么?至于插入,更新,删除示例,请参考如下 sqlCmd:

  1. // insert
  2. NSString * sqlCmd = [NSString stringWithFormat:@"insert into customer (name,address,age) values ('%@','%@',%d)"sqlCmd = [NSString stringWithFormat:@"update customer set address='%@',age=%d where name='%@'"sqlCmd = [NSString stringWithFormat:@"delete from customer where name='%@'"
  3. 查询操作
  4. 查询操作稍微负责一点,需要创建查询描述(sqlite3_stmt),然后调用如下接口编译成字节程序:

  5. sqlITE_API const void *sqlite3_column_blob(sqlite3_stmt*,int iCol);
  6. sqlITE_API int sqlite3_column_bytes(sqlite3_stmt*,0)"> iCol);
  7. sqlITE_API int sqlite3_column_bytes16(sqlite3_stmt*,0)"> iCol);
  8. sqlITE_API double sqlite3_column_double(sqlite3_stmt*,0)"> iCol);
  9. sqlITE_API int sqlite3_column_int(sqlite3_stmt*,0)"> iCol);
  10. sqlITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*,0)"> iCol);
  11. sqlITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*,0)"> iCol);
  12. sqlITE_API const void *sqlite3_column_text16(sqlite3_stmt*,0)"> iCol);
  13. sqlITE_API int sqlite3_column_type(sqlite3_stmt*,0)"> iCol);
  14. sqlITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*,int iCol);
  15. - (NSArray *)queryAllCustomers
  16. {
  17.     NSMutableArray * array = [[NSMutableArray alloc] init];
  18.     
  19.     const char * sqlCmd = "select name,age from customer";
  20.     sqlite3_stmt * statement;
  21.     int state = sqlite3_prepare_v2(database,-1,0)">statement,nil);
  22.     if (state == sqlITE_OK) {
  23.         DLOG(@" >> Succeed to prepare statement. %@"sqlCmd encoding:NSUTF8StringEncoding]);
  24.     }
  25.     
  26.     NSInteger index = 0;
  27.     while (sqlite3_step(statement) == sqlITE_ROW) {
  28.         // get raw data from statement
  29.         //
  30.         char * cstrName = (char *)sqlite3_column_text(statement,0);
  31.         char * cstrAddress = (char *)sqlite3_column_text(statement,1);
  32.         int age = sqlite3_column_int(statement,2);
  33.         
  34.         NSString * name = [NSString stringWithCString:cstrName encoding:NSUTF8StringEncoding];
  35.         NSString * address = [NSString stringWithCString:cstrAddress encoding:NSUTF8StringEncoding];
  36.         KSCustomer * customer = [[KSCustomer alloc]
  37.                                  initWith:name
  38.                                  address:address
  39.                                  age:age];
  40.         [array addObject:customer];
  41.         
  42.         DLOG(@"   >> Record %d : %@ %@ %d",index++sqlite3_finalize(statement);
  43.     
  44.     DLOG(@" >> Query %d records." array;
  45. }
  46. 三,MAC 下查看 sqlite db 文件的工具

  47. MAC 下有一款不错的开源可视化 sqlite db 浏览器:sqlite Database Browser,你可以从以下链接获取

  48. 该软件运行界面如下:

  49. 四,封装 sqlite 的第三方库 FMDB

  50. 在 iOS 中直接使用 sqlite 原生 C 接口还是不那么方便,因此催生了第三方的 iOS 版封装库,其中使用比较广泛又轻量级的就是 FMDB(https://github.com/ccgus/fmdb),目前该库只有六个文件,不超过2000行代码

  51. 使用也是非常简单,在工程中包含这六个文件:

  52. 然后包含头文件

  53. #import "FMDatabase.h"
  54. #import "FMResultSet.h"
  55. #import "FMDatabaseAdditions.h"
  56. 就可以使用该库了:

  57.     // Create database
  58.     //
  59.     NSString * path  = [UIHUtilities configPathFor:kDatabaseFile];
  60.     FMDatabase db  = [[FMDatabase databaseWithPath:path] retain];
  61.     
  62.     if (![db open])
  63.     {  
  64.         DLog(@" >> Error: Failed to open database at %@"
  65.     db.traceExecution = TRUE;
  66. #endif
  67.     
  68.     // Create tables
  69.     //
  70.     [db executeUpdate:@"CREATE TABLE Image (studyUid text,patientId text,seriesUid text,SOPUid text,contentDate text,modality text,patientPosition text,filepath text,thumbnailPath text)"];
  71.     // insert
  72.     //
  73.     BOOL retValue = [db executeUpdate:@"INSERT INTO Image (studyUid,patientId,seriesUid,SOPUid,contentDate,patientPosition,modality,filepath,thumbnailPath) VALUES (?,?,?)"retValue)
  74.         DLog(@" >> Error: Database Failed to insert image %@" ([rs next])
  75.     {
  76.          ....
  77.     }
  78.     // query count
  79.     //
  80.     NSInteger count = 0;
  81.     FMResultSet *rs = [db executeQuery:@"SELECT COUNT(*) FROM Image WHERE seriesUid = ?" ([rs next]) {
  82.         count = [rs intForColumnIndex:0];
  83.     }
  84.     // delete
  85.     //
  86.     retValue = [db executeUpdate:@"DELETE FROM Image WHERE seriesUid = ?"retValue)
  87.         DLog(@" >> Error: Database Failed to delete image by seriesUid %@"

    猜你在找的Sqlite相关文章