ioS学习笔记—— 数据持久化(Sqlite)

1、初始化路径、定义sql语句

        id doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        path=[doc stringByAppendingPathComponent:@"data.sqlite3"];
        
        createSQL = @"CREATE TABLE IF NOT EXISTS Info (name TEXT PRIMARY KEY, age TEXT);";
        loadSQL = @"SELECT name, age FROM Info";
        findSQL = @"SELECT name, age FROM Info WHERE name = ?";
        insetSQL = @"INSERT OR REPLACE INTO Info (name, age) VALUES (?, ?)";
        deleteSQL = @"DELETE FROM Info WHERE name = ? AND age = ?";
        updateSQL = @"UPDATE Info SET name = ?,age = ? WHERE name = ? AND age = ?";


2、打开数据库,建表

- (void)createDatabaseIfNeeded
{
    if (sqlite3_open([path UTF8String], &database) != SQLITE_OK) {
        sqlite3_close(database);
        NSAssert(NO,@"数据库打开失败。");
    }
    else {
        char *err;
        if (sqlite3_exec(database,[createSQL UTF8String],NULL,NULL,&err) != SQLITE_OK) {
            sqlite3_close(database);
            NSAssert1(NO, @"建表失败, %s", err);
        }
        sqlite3_close(database);
    }
}


3、读取所有数据

- (NSMutableArray *)loadFormDatabase
{
    NSMutableArray *ans = [[NSMutableArray alloc] init];
    
    if(sqlite3_open([path UTF8String], &database) != SQLITE_OK){
        sqlite3_close(database);
        NSAssert(NO,@"数据库打开失败。");
        return nil;
    }
    
    sqlite3_stmt *stmt;
    if(sqlite3_prepare_v2(database, [loadSQL UTF8String], -1, &stmt, NULL) == SQLITE_OK){
        while(sqlite3_step(stmt) == SQLITE_ROW){
            char *a = (char *) sqlite3_column_text(stmt, 0);
            char *b = (char *) sqlite3_column_text(stmt, 1);
            
            LLInfo *info = [LLInfo infoMake:[NSString stringWithUTF8String:a] :[NSString stringWithUTF8String:b]];
            
            [ans addObject:info];
        }
    }
    
    sqlite3_finalize(stmt);
    sqlite3_close(database);
    
    return ans;
}


4、插入数据

- (void) insertInfo:(LLInfo *)data
{
    if(sqlite3_open([path UTF8String], &database) != SQLITE_OK){
        sqlite3_close(database);
        NSAssert(NO,@"数据库打开失败。");
        return;
    }
    
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, [insetSQL UTF8String], -1, &stmt, NULL) == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, [[data name] UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 2, [[data age] UTF8String], -1, NULL);
        
        if (sqlite3_step(stmt) != SQLITE_DONE) {
            NSAssert(NO, @"数据插入失败!");
        }
    }
    
    sqlite3_finalize(stmt);
    sqlite3_close(database);
}

5、修改数据

- (void) modifyInfo:(LLInfo *)data :(LLInfo *)newData
{
    if(sqlite3_open([path UTF8String], &database) != SQLITE_OK){
        sqlite3_close(database);
        NSAssert(NO,@"数据库打开失败。");
        return;
    }
    
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, [updateSQL UTF8String], -1, &stmt, NULL) == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, [[newData name] UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 2, [[newData age] UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 3, [[data name] UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 4, [[data age] UTF8String], -1, NULL);
        
        if (sqlite3_step(stmt) != SQLITE_DONE) {
            NSAssert(NO, @"数据修改失败!");
        }
    }
    
    sqlite3_finalize(stmt);
    sqlite3_close(database);
}

6、删除数据

- (void) deleteInfo:(LLInfo *)data
{
    if(sqlite3_open([path UTF8String], &database) != SQLITE_OK){
        sqlite3_close(database);
        NSAssert(NO,@"数据库打开失败。");
        return;
    }
    
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(database, [deleteSQL UTF8String], -1, &stmt, NULL) == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, [[data name] UTF8String], -1, NULL);
        sqlite3_bind_text(stmt, 2, [[data age] UTF8String], -1, NULL);
        
        
        if (sqlite3_step(stmt) != SQLITE_DONE) {
            NSAssert(NO, @"数据删除失败!");
        }
    }
    
    sqlite3_finalize(stmt);
    sqlite3_close(database);
}







你可能感兴趣的:(ios)