设为首页 加入收藏

TOP

iOS sqlite3 的基本使用(增 删 改 查)(二)
2017-10-13 10:33:39 】 浏览:1997
Tags:iOS sqlite3 基本 使用
成功查询,图片为证)

整体代码

//
//  ViewController.m
//  sqlite3
//
//  Created by ma c on 16/5/10.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"
#import <sqlite3.h>
//设置句柄 通过句柄对数据库进行操作
static sqlite3 * db = nil;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置数据库的路径
    NSString * fileName = [[NSSearchPathForDirectoriesInDomains(13, 1, 1)lastObject]stringByAppendingPathComponent:@"text.sqlite"];
    NSLog(@"%@",fileName);
    //打开数据库 如果没有打开的数据库就建立一个
    //第一个参数是数据库的路径 注意要转换为c的字符串
    if (sqlite3_open(fileName.UTF8String, &db) == SQLITE_OK) {
        NSLog(@"打开数据库成功");
        
        //打开数据库成功后建立数据库内的表
        //操作命令的字符串
        //注意字符串的结束处有 ; 号
        NSString * sql = @"create table if not exists t_text (id integer primary key autoincrement,name text);";
        char * errmsg;
        
        sqlite3_exec(db, sql.UTF8String, NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"建表失败 -- %s",errmsg);
        }else{
            NSLog(@"建表成功");
        }
    }else{
        
        NSLog(@"打开数据库失败");
    }
}
- (IBAction)insert:(id)sender {
    
    //为了测试这里利用随机数与for循环进行多次插入不同数据
    
    for (NSInteger i = 0; i < 10; i ++) {//最好先判断能否进入数据库在执行操作 这里偷下懒
        NSString * sql = [NSString stringWithFormat:@"insert into t_text(name) values('%@');",[NSString stringWithFormat:@"旭宝爱吃鱼--%zd",arc4random_uniform(99) ]];
        
        char * errmsg;
        sqlite3_exec(db, sql.UTF8String, NULL, NULL, &errmsg);
        if (errmsg) {
            NSLog(@"插入失败--%s",errmsg);
        }else{
            NSLog(@"插入成功");
        }
    }

}
- (IBAction)delete:(id)sender {
    
    //这里删除随机id 大于3 小于6的
    //操作代码(sql)
    //最好先判断能否进入数据库在执行操作 这里偷下懒
    NSString * sql = @"delete from t_text where id > 3 and id < 6;";
    char * errmsg;
    sqlite3_exec(db, sql.UTF8String, NULL, NULL, &errmsg);
    if (errmsg) {
        NSLog(@"删除失败--%s",errmsg);
    }else{
        NSLog(@"删除成功");
    }
    
}
- (IBAction)update:(id)sender {
    
    //这里吧id为9的 name更改为 hello-world
    //操作代码(sql)
    //最好先判断能否进入数据库在执行操作 这里偷下懒
    NSString * sql = @"update t_text set name = 'hello-world' where id = 9;";
    char * errmsg;
    sqlite3_exec(db, sql.UTF8String, NULL, NULL, &errmsg);
    if (errmsg) {
        NSLog(@"修改失败--%s",errmsg);
    }else{
        NSLog(@"修改成功");
    }
    
}
- (IBAction)select:(id)sender {
    //查询所有信息
    //操作代码(sql)
    //最好先判断能否进入数据库在执行操作 这里偷下懒
    NSString * sql = @"select * from t_text;";
    
    //查询的句柄,游标
    sqlite3_stmt * stmt;
    
    if (sqlite3_prepare(db, sql.UTF8String, -1, &stmt, NULL) == SQLITE_OK) {
        
        //查询数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            //获取表数据的内容
            //sqlite3_column_text('句柄',字段索引值)
            
            NSString * name = [NSString stringWithCString:(const char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
            
            NSLog(@"name = %@",name);
     
        }
    }
}
@end
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS面试题总结(一) 下一篇iOS 学习 - 13.微信分享链接、QQ ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目