数据库操作是我们使用十分频繁的一份操作,在iOS中如何使用数据库,使用什么数据库,是我们不得不考虑的一个问题。

小型数据我们可以使用plist文件,或者NSUserDefaults存储。数据量比较多得情况下,我们可以使用sqlite或者Core Data.

在此先介绍一下sqlite的系统API,然后介绍一下第三方库FMDB,使用第三方库比使用系统的sqlite简单方便。

对数据库的操作,我们可以简单理解为增删改查,下面的具体直接使用代码实现增删改查,不一具体介绍。

#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "Person.h"
@interface ViewController : UIViewController
{
    NSMutableArray *_arrayData;
}
- (IBAction)addButtonClick:(id)sender;
- (IBAction)deleteButtonClick:(id)sender;
- (IBAction)updateButtonClick:(id)sender;
- (IBAction)selectButtonClick:(id)sender;
@end

#import "ViewController.h"

@interface ViewController ()

@end
sqlite3 *_database;
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _arrayData = [[NSMutableArray alloc]init];
    for(int i = 0;i < 5;i ++)
    {
        Person *p = [[Person alloc]init];
        p.name = [NSString stringWithFormat:@"wyg%d",i];
        p.age = 20 + i;
        [_arrayData addObject:p];
    }
    [self createTable];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addButtonClick:(id)sender
{
    [self insertData];
}

- (IBAction)deleteButtonClick:(id)sender
{
    [self deleteData];
}

- (IBAction)updateButtonClick:(id)sender
{
    [self updateData];
}

- (IBAction)selectButtonClick:(id)sender
{
    [self selectData];
}

-(void)openDatabase
{
    if (sqlite3_open([[self getFilePath] UTF8String], &_database) == SQLITE_OK)
    {
        NSLog(@"open success");
    }
    else
    {
        NSLog(@"open failed");
    }
}
-(void)createTable
{
    [self openDatabase];
    NSString *sql = @"create table if not exists students(name text ,age integer)";
    sqlite3_stmt *stmt = nil;
    if (sqlite3_prepare_v2(_database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK)
    {
        if (sqlite3_step(stmt) == SQLITE_DONE)
        {
            NSLog(@"create table success");
        }
        else
        {
            NSLog(@"create table failed");
        }
    }
    sqlite3_finalize(stmt);
    sqlite3_close(_database);
}
-(void)insertData
{
    [self openDatabase];
    NSString *sql = @"insert into students(name,age)values(?,?)";
    sqlite3_stmt *stmt = nil;
    for (int i = 0; i < _arrayData.count; i ++)
    {
        Person *p = _arrayData[i];
        if (sqlite3_prepare_v2(_database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK)
        {
                sqlite3_bind_text(stmt, 1, [p.name UTF8String], strlen([p.name UTF8String]), nil);
                sqlite3_bind_int(stmt, 2, p.age);
                if (sqlite3_step(stmt) == SQLITE_DONE)
                {
                    NSLog(@"insert success");
                }
            else
            {
                NSLog(@"insert failed");
            }
        }
    }
    sqlite3_finalize(stmt);
    sqlite3_close(_database);
}
-(void)deleteData
{
    [self openDatabase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"delete from students where age > 23";
    if (sqlite3_prepare_v2(_database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK)
    {
        if (sqlite3_step(stmt) == SQLITE_DONE)
        {
            NSLog(@"delete success");
        }
        else
        {
            NSLog(@"delete failed");
        }
    }
    sqlite3_finalize(stmt);
    sqlite3_close(_database);
}
-(void)updateData
{
    [self openDatabase];
    sqlite3_stmt *stmt = nil;
    NSString *sql = @"update students set name = 'www' where age > 22";
    if (sqlite3_prepare_v2(_database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK)
    {
        if (sqlite3_step(stmt) == SQLITE_DONE)
        {
            NSLog(@"update success");
        }
        else
        {
            NSLog(@"update failed");
        }
    }
    sqlite3_finalize(stmt);
    sqlite3_close(_database);
}
-(void)selectData
{
    [self openDatabase];
    NSString *sql = @"select *from students";
    sqlite3_stmt *stmt = nil;
    NSMutableArray *array = [[NSMutableArray alloc]init];
    if (sqlite3_prepare_v2(_database, [sql UTF8String], -1, &stmt, nil) == SQLITE_OK)
    {
        while (sqlite3_step(stmt) == SQLITE_ROW)
        {
            char *name = (char *)sqlite3_column_text(stmt, 0);
            int age = sqlite3_column_int(stmt, 1);
            Person *p = [[Person alloc]init];
            p.name = [NSString stringWithUTF8String:name];
            p.age = age;
            [array addObject:p];
        }
    }
    NSLog(@"---%@",array);
    sqlite3_finalize(stmt);
    sqlite3_close(_database);
}
-(NSString *)getFilePath
{
    NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [docuPath stringByAppendingPathComponent:@"database.db"];
    return filePath;
}
@end
sqlite

相关文章: