欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS Sqlite3数据库简明教程

程序员文章站 2022-05-28 18:03:21
...

1,导入支持sqlite3的本地frame: libsqlite3.tbd (Xcode 7及更高版本后缀为 .tbd ,Xcode 6 及之前版本后缀为 .dylib)

2,导入头文件并设置所需属性:

#import "ViewController.h"
#import "Person.h"
#import "sqlite3.h"

@interface ViewController ()

@property (nonatomic,assign) sqlite3 *sqlite3;
@property (nonatomic,strong) NSArray *dataList;

3,创建数据库,如果要打开某个数据库文件也用此方法,有此文件名会打开,没有默认创建:

#pragma mark_Sqlite3Open
- (void)openDatabase {
    //1.设置文件名
    NSString *filename = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.db"];
    //2.打开数据库文件,如果没有会自动创建一个文件
    NSInteger result = sqlite3_open(filename.UTF8String, &_sqlite3);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据库成功!");
        //3.创建一个数据库表
        char *errmsg = NULL;
        sqlite3_exec(_sqlite3, "CREATE TABLE IF NOT EXISTS t_person(id integer primary key autoincrement, name text, age integer)", NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"错误:%s", errmsg);
        } else {
            NSLog(@"创表成功!");
        }
    } else {
        NSLog(@"打开数据库失败!");
    }
}

4,往数据库中录入数据:

#pragma mark_Sqlite3Insert
- (void)insertData {
    
    NSString *nameStr;
    NSInteger age;
    for (NSInteger i = 0; i < 1000; i++) {
        nameStr = [NSString stringWithFormat:@"Bourne-%d", arc4random_uniform(1000)];
        age = arc4random_uniform(80) + 20;
        NSString *sql = [NSString stringWithFormat:@"INSERT INTO t_person (name, age) VALUES('%@', '%ld')", nameStr, age];
        char *errmsg = NULL;
        sqlite3_exec(_sqlite3, sql.UTF8String, NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"错误:%s", errmsg);
        }
    }
    NSLog(@"插入完毕!");
}

5,读取数据库:

     5.1,创建一个Person类:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Person : NSObject<NSCoding>

//@property (nonatomic,strong) UIImage *img;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;

+(in)personWithName:(NSString *)name Age:(NSInteger)age;

@end

     5.2,实现Person中的方法:

#import "Person.h"

@implementateion Person

+(instancetype)personWithName:(NSString *)name Age:(NSInteger)age{
    
    Person *person = [[Person alloc]init];
    person.name = name;
    person.age = age;

    return person;
    
}

@end

    5.3,读取数据库中的数据:

#pragma mark_Sqlite3Read
- (void)readData {
    
    //读取数据库中数据之前,需要先打开数据库
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1000];
    char *sql = "select name, age from t_person;";
    sqlite3_stmt *stmt;
    NSInteger result = sqlite3_prepare_v2(_sqlite3, sql, -1, &stmt, NULL);
    
    if (result == SQLITE_OK) {
         NSLog(@"STMT:%d",sqlite3_step(stmt));
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            char *name = (char *)sqlite3_column_text(stmt, 0);
            NSInteger age = sqlite3_column_int(stmt, 1);
            
            //创建对象
            
            Person *person = [Person personWithName:[NSString stringWithUTF8String:name] Age:age];
            
           //
            NSLog(@"Name:%@ Age:%ld PName:%@ Page:%ld",[NSString stringWithUTF8String:name],age,person.name,person.age);
            
            [mArray addObject:person];
        }
        self.dataList = mArray;
    }
    sqlite3_finalize(stmt);
    
    
    NSLog(@"DataList:%@",self.dataList);
}

注:读数据库时一定要先打开数据库

转载于:https://my.oschina.net/Kuture/blog/693337