• iOS---iOS中SQLite的使用


    一.SQLite的使用

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

    第一步:导入头文件

    • 需要添加SQLite相关的库以及头文件:在项目文件的Build Phases下,找到Link Binary Library(ies),添加libsqlite3.0.dylib(libsqlite3.dylib与前者的区别暂时不知,两者应该差不多);在项目文件中头文件或者源文件中添加头文件#import "/usr/include/sqlite3.h"

    第二步:开始使用SQLite:

    1.打开数据库。

    • 使用前注意:如果不往数据库里面添加任何的表,这个数据库等于没有建立,不会在硬盘上产生任何文件,如果数据库已经存在,则会打开这个数据库。
    NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES);
    NSString *databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"mydb"];
    //上面两句已经比较熟悉了吧!
    //打开数据库
    if (sqlite3_open([databaseFilePath UTF8String], &database)==SQLITE_OK) {
            NSLog(@"sqlite dadabase is opened.");
    }
    else{ return;}//打开不成功就返回
    

    2.在打开了数据库的前提下,如果数据库没有表,那就开始建表了哦!

    char *error;
    const char *createSql="create table(id integer primary key autoincrement, name text)";
    if (sqlite3_exec(database, createSql, NULL, NULL, &error)==SQLITE_OK) {
            NSLog(@"create table is ok.");
    }
    else
    {
           NSLog(@"error: %s",error);
           sqlite3_free(error);//每次使用完毕清空error字符串,提供给下一次使用
    }
    

    3.建表完成之后,就开始插入记录:

    const char *insertSql="insert into a person (name) values(‘gg’)";
    if (sqlite3_exec(database, insertSql, NULL, NULL, &error)==SQLITE_OK) {
            NSLog(@"insert operation is ok.");
    }
    
    else
    {
           NSLog(@"error: %s",error);
           sqlite3_free(error);//每次使用完毕清空error字符串,提供给下一次使用
    }
    

    第三步,查询记录:

    const char *selectSql="select id,name from a person";
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(database,selectSql, -1, &statement, nil)==SQLITE_OK) {
            NSLog(@"select operation is ok.");
    }
    else
    {
           NSLog(@"error: %s",error);
           sqlite3_free(error);
    }
    while(sqlite3_step(statement)==SQLITE_ROW) {
    int _id=sqlite3_column_int(statement, 0);
    NSString *name=(char*)sqlite3_column_text(statement, 1);
    NSLog(@"row>>id %i, name %s",_id,name);
    }
    sqlite3_finalize(statement);
    

    最后,关闭数据库:

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

    二.FMDB的使用

    • github地址是https://github.com/ccgus/fmdb
    // 继承
    # pod 'FMDB/SQLCipher'
    

    1.FMDB的创建:

        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shops.sqlite"];
        self.db = [FMDatabase databaseWithPath:path];
    

    2.FMDB的打开

        [self.db open];
    

    3.创表

    • 创建表
        [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY, name text NOT NULL, price real);"];
    

    4.表操作

    • 表的一些操作
         // executeQuery:查询数据
        // [self.db executeQuery:NSString *];
    
        // executeUpdate:除查询数据以外的其他操作
        // [self.db executeUpdate:NSString *];
    
    • 删除表中数据
    [self.db executeUpdate:@"DELETE FROM t_shop WHERE price < 800;"];
    
    • 表中数据查询
        FMResultSet *set = [self.db executeQuery:@"SELECT * FROM t_shop;"];
    
        // 不断往下取数据
        while (set.next) {
            // 获得当前所指向的数据
            NSString *name = [set stringForColumn:@"name"];
            double price = [set doubleForColumn:@"price"];
            NSLog(@"%@ %f", name, price);
        }
    
    • 查询数据
        for (int i = 0; i<100; i++) {
            NSString *name = [NSString stringWithFormat:@"手机-%d", i];
    #warning 这里的字符串不用再加上''
            [self.db executeUpdateWithFormat:@"INSERT INTO t_shop(name, price) VALUES (%@, %d);", name, arc4random()%1000];
        }
    
  • 相关阅读:
    jquery中,input获得焦点时光标自动定位到文字后面
    微信接口调用
    bootstrap-datetimepicker插件双日期的设置
    input输入框在移动端点击有阴影解决方法
    input输入框光标高度问题
    Appendix 2- Lebesgue integration and Reimann integration
    Appendix 1- LLN and Central Limit Theorem
    LESSON 7- High Rate Quantizers and Waveform Encoding
    LESSON 6- Quantization
    LESSON 5
  • 原文地址:https://www.cnblogs.com/ShaoYinling/p/4817824.html
Copyright © 2020-2023  润新知