zoukankan      html  css  js  c++  java
  • 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);
        
        
    
        
        
    }
    

      

  • 相关阅读:
    c# gdi设置画刷透明
    char,varchar,nvarchar,text区别与联系
    banner无缝轮播【小封装】
    div中的内容垂直居中的五种方法
    jQuery hover() 方法
    年过三十,我为什么要学习ios 与安卓App 移动端技术
    CentOS 中用 Split 命令分割文件的方法
    centos E440 安装无线网卡
    CentOS7修改默认运行级别
    iwconfig: command not found 解决方案
  • 原文地址:https://www.cnblogs.com/Moshimol/p/5697053.html
Copyright © 2011-2022 走看看