zoukankan      html  css  js  c++  java
  • iOS使用sqlite3原生语法进行增删改查以及FMDB的使用

    首先要导入libsqlite3.dylib并且加入头文件#import <sqlite3.h>,在进行增删改查之前还要先把数据库搞进去。

    一种方法是从外面拷贝到程序里:http://www.cnblogs.com/Steak/p/3764395.html

    另一种方法就是直接创建表,既然是操纵数据库,那么一定有一个数据库对象,sqlite是C库,所以需要一个C变量:

    1 @interface DBHandler () {
    2     sqlite3 *db;
    3 }

    在沙盒Documents文件夹里创建一个数据库文件,如果数据库不存在,sqlite3_open函数则会创建一个文件:

    1     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    2     NSString *dataBasePath = [[NSString alloc] initWithString:[documentPath stringByAppendingPathComponent:@"sqlite.sqlite"]];
    3     int result = sqlite3_open([dataBasePath UTF8String], &db);

    返回值是一个宏定义,如果成功则是SQLITE_OK,需要注意的是每次执行数据库操作的时候都需要open,完事之后都要调用对应的close。

    对于打开和关闭的时机,有两种说法,一种是始终保持打开,另一种说法是随用随开关,个人比较倾向后者。

    创建表:

    1     char *sql = "create table if not exists for_persons (id integer primary key autoincrement, name text, age integer);";
    2     char *error;
    3     int result = sqlite3_exec(db, sql, NULL, NULL, &error);

    增删改:

     1     char *sql = "insert into for_persons(name, age) values(?, ?)";
     2     
     3     sqlite3_stmt *stmt;
     4     int result = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
     5     if (result == SQLITE_OK) {
     6         sqlite3_bind_text(stmt, 1, "xiaoming", -1, NULL);
     7         sqlite3_bind_int(stmt, 2, 10);
     8 
     9         if (sqlite3_step(stmt)  == SQLITE_DONE) {
    10             //成功
    11         }
    12         else {
    13             //失败
    14         }
    15     }
    16     else {
    17         //语法检查失败
    18     }
    19     
    20     sqlite3_finalize(stmt);

    这里以增加为例子,增删改都要定义一个sqlite3_stmt,并且在最后释放掉,然后通过sqlite3_prepare_v2对sql进行检查,检查成功后通过sqlite3_bind_XXX方法进行数据绑定,然后通过sqlite3_step执行sql语句。增删改的格式都是一样的。 

    查询数据:

     1     char *sql = "select * from for_persons";
     2     sqlite3_stmt *stmt;
     3     int result = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
     4     if (result == SQLITE_OK) {
     5         while (sqlite3_step(stmt) == SQLITE_ROW) {
     6             int ID = sqlite3_column_int(stmt, 0);
     7             char *name = (char *)sqlite3_column_text(stmt, 1);
     8             int age = sqlite3_column_int(stmt, 2);
     9             
    10             //到这里已经完整读出了一条记录
    11             //如果有多条数据,可以在外面定义数组保存
    12         }
    13     }

    查询也类似,需要使用while (sqlite3_step(stmt) == SQLITE_ROW)来判断如果还有下一条就取出来。

    以上是原生sqlite3最基本的使用,但是这种C风格的代码看着就反胃,复制粘贴都没有胃口。

    还好有人封装了一个很方便的第三方库叫FMDB。下面说说FMDB的使用,首先下载FMDB:https://github.com/ccgus/fmdb,同样导入libsqlite3.dylib,并且#import "FMDatabase.h"。

    创建库文件并打开的代码很类似,不过都是OC代码,看起来舒服多了:

    1 @interface DBHandler () {
    2     FMDatabase *db;
    3 }
    1     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    2     NSString *dataBasePath = [[NSString alloc] initWithString:[documentPath stringByAppendingPathComponent:@"sqlite.sqlite"]];
    3     db = [FMDatabase databaseWithPath:dataBasePath];
    4     [db open];

    创建表:

    1     NSString *sql = @"create table if not exists for_persons (id integer primary key autoincrement, name text, age integer);";
    2     [db executeUpdate:sql];

    代码一下子简单了,executeUpdate返回一个BOOL值。

    增删改:

    1     [db executeUpdate:@"insert into for_persons(name, age) values(?, ?)", @"xiaowang", @20];

    同样都是使用executeUpdate方法,这里的问号相当于NSString当中的@,是个OC对象占位符。

    执行查询也是OC风格的:

    1     FMResultSet *rs = [db executeQuery:@"select name, age from for_persons where name = ?", @"xiaowang"];
    2     while ([rs next]) {
    3         NSString *name = [rs stringForColumn:@"Name"];
    4         int age = [rs intForColumn:@"Age"];
    5         NSLog(@"%@%d", name, age);
    6     }
    7     [rs close];
  • 相关阅读:
    【转】Android——设置颜色的三种方法
    Eclipse Android安装APP时覆盖安装问题
    自定义数组,实现输出改数组的长度、最大值和最小值
    用程序实现对数组a[45,96,78,6,18,66,50]中的元素进行排序
    PHP面试题2
    PHP面试题
    gulp
    移动端base.css
    笔记
    mouseover和mouseout事件在鼠标经过子元素时也会触发
  • 原文地址:https://www.cnblogs.com/Steak/p/3802508.html
Copyright © 2011-2022 走看看