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++深拷贝与浅拷贝
    c++构造函数的explicit
    c++虚函数和虚函数表
    c++重载、重写、隐藏(重定义)
    c++传值、传指针、传引用
    ASP.Net Core API 学习の中间件
    WPF中String Format的用法
    ASP.Net Core API 全局处理异常
    989. Add to Array-Form of Integer
    1014. Best Sightseeing Pair
  • 原文地址:https://www.cnblogs.com/Moshimol/p/5697053.html
Copyright © 2011-2022 走看看