iOS数据库操作流程

  SQLite最新的版本是3.0,使用之前应该先导入libsqlite3.0.dylib

  1.导入流程

    

   

 

  2.iOS中操作数据库的流程

    打开数据库

    准备SQL数据库

    执行SQL数据库

    语句完结

    关闭数据库

  3.SQLite()使用的是C的函数接口

    

  4.创建数据库

  

-(void)createSQL{
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:kDataBaseFilePath]) {
        NSLog(@"数据库不存在");
        return;
    }
    NSLog(@"%@",kDataBaseFilePath);
    [manager createFileAtPath:kDataBaseFilePath contents:nil attributes:nil];
    
    
    sqlite3 *sql =NULL;
    int openDbResult = sqlite3_open([kDataBaseFilePath UTF8String], &sql);
    
    if (openDbResult==SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }
    else{
    
        NSLog(@"打开不成功");
        
        [manager removeItemAtPath:kDataBaseFilePath error:nil];
        return;
    }
    
    
     NSString *sqlString = @"CREATE TABLE user(id integer PRIMARY KEY AUTOINCREMENT, username text NOT NULL UNIQUE, password text NOT NULL);";
    
    
    
    
    
    char *errmsg = NULL;
    int exeResult = sqlite3_exec(sql, [sqlString UTF8String], NULL, NULL, &errmsg);
    
    if (exeResult == SQLITE_OK) {
        NSLog(@"表格成功被创建");
        
    }
    else{
    
        NSLog(@"失败");
        sqlite3_close(sql);
        [manager removeItemAtPath:kDataBaseFilePath error:nil];
        return;
    }
    sqlite3_close(sql);
    
    

    
    
}
  5.数据库的插入数据库

   

-(void)createSQL{
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:kDataBaseFilePath]) {
        NSLog(@"数据库不存在");
        return;
    }
    NSLog(@"%@",kDataBaseFilePath);
    [manager createFileAtPath:kDataBaseFilePath contents:nil attributes:nil];
    
    
    sqlite3 *sql =NULL;
    int openDbResult = sqlite3_open([kDataBaseFilePath UTF8String], &sql);
    
    if (openDbResult==SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }
    else{
    
        NSLog(@"打开不成功");
        
        [manager removeItemAtPath:kDataBaseFilePath error:nil];
        return;
    }
    
    
     NSString *sqlString = @"CREATE TABLE user(id integer PRIMARY KEY AUTOINCREMENT, username text NOT NULL UNIQUE, password text NOT NULL);";
    
    
    
    
    
    char *errmsg = NULL;
    int exeResult = sqlite3_exec(sql, [sqlString UTF8String], NULL, NULL, &errmsg);
    
    if (exeResult == SQLITE_OK) {
        NSLog(@"表格成功被创建");
        
    }
    else{
    
        NSLog(@"失败");
        sqlite3_close(sql);
        [manager removeItemAtPath:kDataBaseFilePath error:nil];
        return;
    }
    sqlite3_close(sql);
    
    

    
    
}

  

转载于:https://www.cnblogs.com/Moshimol/p/5697053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值