SQLite 数据库存储

前言

  • 采用 SQLite 数据库来存储数据。SQLite 作为一中小型数据库,应用 iOS 中,跟前三种保存方式相比,相对比较复杂一些。

  • 注意:写入数据库,字符串可以采用 char 方式,而从数据库中取出 char 类型,当 char 类型有表示中文字符时,会出现乱码。这是因为数据库默认使用 ASCII 编码方式。所以要想正确从数据库中取出中文,需要用 NSString 来接收从数据库取出的字符串。

  • sqlite 的方法:

    • Objective-C

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      sqlite3 *db             数据库句柄,跟文件句柄很类似
      sqlite3_stmt *stmt 这个相当于 ODBC 的 Command 对象,用于保存编译好的 SQL 语句
      sqlite3_open() 打开数据库,没有数据库时创建。
      sqlite3_exec() 执行非查询的 sql 语句
      Sqlite3_step() 在调用 sqlite3_prepare 后,使用这个函数在记录集中移动。
      Sqlite3_close() 关闭数据库文件

      还有一系列的函数,用于从记录集字段中获取数据,如:

      sqlite3_column_text() 取 text 类型的数据。
      sqlite3_column_blob() 取 blob 类型的数据
      sqlite3_column_int() 取 int 类型的数据
    • Swift

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      sqlite3 *db             数据库句柄,跟文件句柄很类似
      sqlite3_stmt *stmt 这个相当于 ODBCCommand 对象,用于保存编译好的 SQL 语句
      sqlite3_open() 打开数据库,没有数据库时创建。
      sqlite3_exec() 执行非查询的 sql 语句
      Sqlite3_step() 在调用 sqlite3_prepare 后,使用这个函数在记录集中移动。
      Sqlite3_close() 关闭数据库文件

      还有一系列的函数,用于从记录集字段中获取数据,如:

      sqlite3_column_text() 取 text 类型的数据。
      sqlite3_column_blob() 取 blob 类型的数据
      sqlite3_column_int() 取 int 类型的数据

1、环境配置

  • 添加动态库

    • 在 TARGETS => Build Phases => Link Binary With Libraries => +(添加) => Add Other… => command + shit + g => 输入 /usr/lib 找到一下文件并添加:libsqlite3.0.dylib

      SQLite1

      SQLite2

    • 或者在 TARGETS -> Build Settings -> Linking -> Other Linker Flags 中添加 -l< 所需 dylib 的名称 >:-lsqlite3.0

      SQLite3

    • 添加头文件:

      • Objective-C

        1
        #import "sqlite3.h"
  • 配置数据库路径

    • Objective-C

      1
      2
      3
      4
      5
      6
      7
      // 设置数据库文件路径
      NSString *databaseFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/mydb.sqlite"];

      // 创建数据库句柄
      sqlite3 *db;

      char *error;

2、打开数据库

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    // 打开数据库,数据库文件不存在时,自动创建文件

    if (sqlite3_open([databaseFilePath UTF8String], &db) == SQLITE_OK) {
    NSLog(@"sqlite dadabase is opened.");
    } else {
    NSLog(@"sqlite dadabase open fail.");
    }

3、创建数据表

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    /*
    sql 语句,专门用来操作数据库的语句。
    create table if not exists 是固定的,如果表不存在就创建。
    myTable() 表示一个表,myTable 是表名,小括号里是字段信息。
    字段之间用逗号隔开,每一个字段的第一个单词是字段名,第二个单词是数据类型,primary key 代表主键,autoincrement 是自增。
    */

    NSString *createSql = @"create table if not exists myTable(id integer primary key autoincrement, name text, age integer, address text)";

    if (sqlite3_exec(db, [createSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
    NSLog(@"create table is ok.");
    } else {
    NSLog(@"error: %s", error);

    // 每次使用完毕清空 error 字符串,提供给下一次使用
    sqlite3_free(error);
    }

4、插入记录

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    NSString *insertSql = @"insert into myTable(name, age, address) values('小新', '8', '东城区')";

    if (sqlite3_exec(db, [insertSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
    NSLog(@"insert operation is ok.");
    } else {
    NSLog(@"error: %s", error);

    // 每次使用完毕清空 error 字符串,提供给下一次使用
    sqlite3_free(error);
    }

5、修改记录

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    NSString *updateSql = @"update myTable set name = '小白', age = '10', address = '西城区' where id = 2";

    if (sqlite3_exec(db, [updateSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
    NSLog(@"update operation is ok.");
    } else {
    NSLog(@"error: %s", error);

    // 每次使用完毕清空 error 字符串,提供给下一次使用
    sqlite3_free(error);
    }

6、删除记录

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    NSString *deleteSql = @"delete from myTable where id = 3";

    if (sqlite3_exec(db, [deleteSql UTF8String], NULL, NULL, &error) == SQLITE_OK) {
    NSLog(@"delete operation is ok.");
    } else {
    NSLog(@"error: %s", error);

    // 每次使用完毕清空 error 字符串,提供给下一次使用
    sqlite3_free(error);
    }

7、查询记录

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    sqlite3_stmt *statement;

    // @"select * from myTable" 查询所有 key 值内容
    NSString *selectSql = @"select id, name, age, address from myTable";

    if (sqlite3_prepare_v2(db, [selectSql UTF8String], -1, &statement, nil) == SQLITE_OK) {

    while(sqlite3_step(statement) == SQLITE_ROW) {

    // 查询 id 的值
    int _id = sqlite3_column_int(statement, 0);

    // 查询 name 的值
    NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];

    // 查询 age
    int age = sqlite3_column_int(statement, 2);

    // 查询 name 的值
    NSString *address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];

    NSLog(@"id: %i, name: %@, age: %i, address: %@", _id, name, age, address);
    }
    } else {
    NSLog(@"select operation is fail.");
    }

    sqlite3_finalize(statement);

8、关闭数据库

  • Objective-C

    1
    sqlite3_close(db);
文章目录
  1. 1. 前言
  2. 2. 1、环境配置
  3. 3. 2、打开数据库
  4. 4. 3、创建数据表
  5. 5. 4、插入记录
  6. 6. 5、修改记录
  7. 7. 6、删除记录
  8. 8. 7、查询记录
  9. 9. 8、关闭数据库
隐藏目录