美文网首页
ios数据存储--SQLite的简单使用

ios数据存储--SQLite的简单使用

作者: Hyman0819 | 来源:发表于2016-09-24 18:46 被阅读66次

    第一步:添加文库libsqlite3.tbd

    添加库文件:libsqlite3.tbd 输入sqlite,选择 libsqlite3.tbd

    第二步:整个viewController.m文件

    //
    //  ViewController.m
    //  sqlliteTest01
    //
    //  Created by qianfeng on 16/9/24.
    //  Copyright © 2016年 qianfeng. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    {
        sqlite3 *_DB;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self openDB];
        [self createTABLE];
        [self insertDATA];
        [self findDATA];
    }
    
    
    /*
     1.打开数据库
     */
    -(void)openDB
    {
         //数据库保存路径
        NSArray *array = NSSearchPathForDirectoriesInDomains(
    NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPath = [array lastObject];
        NSLog(@"数据库路径= %@",documentPath);
        
        NSString *dataBasePath = [documentPath
    stringByAppendingPathComponent:@"sql.db"];
    
       // open数据库(不存在则创建)
        int result = sqlite3_open([dataBasePath UTF8String], &_DB);
        if (result == SQLITE_OK) {
            NSLog(@"数据库打开成功");
        }else{
            NSLog(@"数据库打开失败");
        }
    }
    
    
    /*
     2.创建表
     */
    -(void)createTABLE
    {
        char *errorMsg;
        NSString *sql = @"create table if not exists user
    (u_id integer primary key autoincrement,name text,password text)";
        
        int result = sqlite3_exec(_DB, [sql UTF8String], NULL, NULL, 
    &errorMsg);
        if ( result == SQLITE_OK) {
            NSLog(@"表创建成功");
        }else{
            NSLog(@"表创建失败 = %s",errorMsg);
            sqlite3_free(errorMsg);
        }
    }
    
    
    /*
     3.插入数据
     */
    -(void)insertDATA
    {
        NSString *sql_insert = [NSString stringWithFormat:
    @"insert into user(name,password) values(?,?);"];
    
        //检查sql语句
        sqlite3_stmt *stmp;
        int result = sqlite3_prepare_v2(_DB, [sql_insert UTF8String], 
    -1, &stmp, NULL);
    
        if (result == SQLITE_OK) {
            NSLog(@"将要执行的插入语句正确");
            //绑定参数(下标从1开始,-1代表字符串长度)
            sqlite3_bind_text(stmp, 1, "zh123456", -1, NULL);
            sqlite3_bind_text(stmp, 2, "mm123456", -1, NULL);
            
            //
            int result2 = sqlite3_step(stmp);
            if (result2 == SQLITE_DONE) {
                NSLog(@"插入成功");
            }else{
                NSLog(@"插入失败");
            }
            
            
        }else{
            NSLog(@"将要执行的插入语句错误");
        }//end (if)
    }
    
    /*
     4.查询数据
     */
    -(void)findDATA
    {
        NSString *sql_select = @"select * from user";
        
        sqlite3_stmt *stmt;
        int result = sqlite3_prepare_v2(_DB, [sql_select UTF8String], 
    -1, &stmt, NULL);
    
        if (result == SQLITE_OK) {
            NSLog(@"查询SQL语法正确");
            while (sqlite3_step(stmt) == SQLITE_ROW) {
                //查询的列是从0开始,插入的列是从1开始
                int u_id = sqlite3_column_int(stmt, 0);
                char *name = (char *)sqlite3_column_text(stmt, 1);
                char *password = (char *)sqlite3_column_text(stmt, 2);
                NSLog(@"u_id = %i ,name = %s ,
                              password = %s",u_id,name,password);
            }
    
        }else{
            NSLog(@"查询SQL语法有误");
        }
    }
    @end
    

    |
    |


    最后:运行即可. . .

    |
    |

    微云网盘:sqliteTest1

    相关文章

      网友评论

          本文标题:ios数据存储--SQLite的简单使用

          本文链接:https://www.haomeiwen.com/subject/ygbxyttx.html