行业动态
iOS开发中sqlite和开源库FMDB的使用地址
2021-01-23

关于sqlite是一个轻量级ios 查看sqlite数据库,跨平台的开源数据库引擎。它在读写效率,易操作性和内存消耗方面具有很大的优势,因此在移动开发人员中非常受欢迎。当然,由于SQLite追求简单性和效率,它还限制了对并发和海量数据的处理。该博客主要讨论在IOS开发中使用sqlite和开源库FMDB。

演示地址TP

首先打开数据库

int result = sqlite3_open_v2(fileName.UTF8String, &db, SQLITE_IOERR_READ|SQLITE_IOERR_WRITE|SQLITE_OPEN_CREATE, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"open true");
    }

2.创建表格

//创建表
    const char *sqlCreate = "CREATE TABLE IF NOT EXISTS t_demo (id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL,age integer NOT NULL);";
    
    char *errmsg = NULL;
    result = sqlite3_exec(db, sqlCreate, NULL, NULL, &errmsg);
    if (result == SQLITE_OK) {
        NSLog(@"create true");
    }

3.执行sql语句

//执行sql语句
    
    //1.插入
    NSString *sqlInsert = [NSString stringWithFormat:@"INSERT INTO t_demo (name,age) VALUES('%@','%d');",@"one",19];
    sqlite3_exec(db, sqlInsert.UTF8String, NULL, NULL, &errmsg);
    sqlite3_exec(db, sqlInsert.UTF8String, NULL, NULL, &errmsg);
    sqlite3_exec(db, sqlInsert.UTF8String, NULL, NULL, &errmsg);
    if (errmsg) {
        NSLog(@"insert false");
    }
    
    // 2.查询
    NSString *sqlQuery = [NSString stringWithFormat:@"SELECT id,name,age FROM t_demo;"];
    sqlite3_stmt *stmt = NULL;
    //准备执行语句
    if (sqlite3_prepare_v2(db, sqlQuery.UTF8String, -1, &stmt, NULL) == SQLITE_OK) {
        //分步执行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int ID = sqlite3_column_int(stmt, 0);
            const unsigned char *name  = sqlite3_column_text(stmt, 1);
            int age = sqlite3_column_int(stmt, 2);
            
            NSLog(@"%d***%s******%d",ID,name,age);
        }
    }
    //释放
    sqlite3_free(stmt);


393701618