FMDataBase数据库的创建和插入

来源:互联网 发布:ubuntu查看samba用户 编辑:程序博客网 时间:2024/06/05 21:53

添加fmdb文件第三方库  

导入系统库 libsqlite3.dylib

<span style="background-color: rgb(255, 255, 0);"><span style="font-size:18px;">#import "FMDatabase.h"</span></span>

定义 FMDatabase
FMDatabase *_dataBase;//也封装了sqlite3的事务操作


<span style="font-size:18px;">       //创建数据库的路径    [NSHomeDirectory() 是应用程序根目录    NSString *dbPath = [NSHomeDirectory() stringByAppendingString:@"/Documents/test.db"];        _dataBase = [[FMDatabase alloc] initWithPath:dbPath];    if ([_dataBase open]) {       //创建表        NSString *createSql = @"create table if not exists student(id integer,name varchar(256))";        if (![_dataBase executeUpdate:createSql]) {            NSLog(@"create error:%@",_dataBase.lastErrorMessage);        }    }    //函数执行的时间->时间差    NSDate *date1 = [NSDate date];   //手动开启一个事物,插入1000跳数据    [self insertDataWithCount:1000 isUseTransaction:YES];    NSDate *date2 =[NSDate date];    //算时间差 (date2 与date1 相差的秒数)    NSTimeInterval time = [date2 timeIntervalSinceDate:date1];    NSLog(@"Interval:%f",time);}                                                                                                                                         </span>


<span style="font-size:18px;"> //向表中插入1000条数据,是否启用事务- (void)insertDataWithCount:(NSInteger)count isUseTransaction:(BOOL)isuse{    if (isuse) {      //手动开启事务        //oc中对异常的捕获代码,性能不高,不能在程序中,频繁使用        BOOL isError = NO;//用于标识是否出现异常        @try {            //写可能抛出异常的代码            //手动开启一个事务,插入1000条数据的操作,算作一个事务            [_dataBase beginTransaction];//开启事务,内部会操作sqlite3来在数据库中开启一个事务            for (int i =0; i<count; i++) {                NSString *stId = [NSString stringWithFormat:@"%d",i];                NSString *stName = [NSString stringWithFormat:@"student%d",i];                NSString *insertSql = @"insert into student(id,name) values(?,?)";                BOOL isSuccessed = [_dataBase executeUpdate:insertSql,stId,stName];                if (!isSuccessed) {                    NSLog(@"insert error:%@",_dataBase.lastErrorMessage);                }            }        }        @catch (NSException *exception) {            //try中的代码,出现异常(可能引起程序崩溃的异常),会执行到catch中            //NSException 异常的类            isError = YES;            NSLog(@"exception:%@",exception.reason);            //回滚事务,回到最初的状态            [_dataBase rollback];        }        @finally {            //无论代码是否出现异常,都会走到finally中            if (isError == NO) {               //提交事务                [_dataBase commit];            }        }    }else{      //常规的操作        for (int i =0; i<count; i++) {            NSString *stId = [NSString stringWithFormat:@"%d",i];            NSString *stName = [NSString stringWithFormat:@"student%d",i];            NSString *insertSql = @"insert into student(id,name) values(?,?)";            BOOL isSuccessed  =[_dataBase executeUpdate:insertSql,stId,stName];            if (!isSuccessed) {                NSLog(@"insert error:%@",_dataBase.lastErrorMessage);            }        }    }}</span>



<span style="font-size:18px;"></span>
0 0
原创粉丝点击