微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlite1-1

//
//  ViewController.m
//  sqliteText
//
//  Created by jerehedu on 15/2/2.
//  copyright (c) 2015年 baidu. All rights reserved.
//

#import "ViewController.h"
#import <sqlite3.h>  //1. 导入sqlite3文件

@interface ViewController ()

@end

@implementation ViewController
{
    sqlite3 *db;//2. 声明sqlite3对象
}



- (Nsstring *)getUserDocumentPath
{
    //3. 转换沙盒路径为C字符串
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    Nsstring *documentPath=[path lastObject];
    return documentPath;
}

- (Nsstring *)appendingPathComponent:(Nsstring *)documentPath andFileName:(Nsstring*)fileName
{
    Nsstring *dbPath = [documentPath stringByAppendingPathComponent:fileName];
    return dbPath;
}
- (BOOL)openorCreatesqliteDBWithDBPath:(Nsstring*)dbPath
{
    
    const char *p = [dbPath UTF8String];
    
    //4. 打开或创建数据库串
    int res = sqlite3_open(p,&db);
    if (res == sqlITE_OK) {
        return YES;
    }
    
    return NO;
}
#pragma mark 增 删 改
- (BOOL)execsqlNoQueryWith:(Nsstring*)sql
{
    int insert_res = sqlite3_exec(db,[sql UTF8String],NULL,NULL);
    if (insert_res == sqlITE_OK) {
        return YES;
    }
    return NO;
}
// <1>.......
- (sqlite3_stmt*)execQueryWithsql:(Nsstring*)sql
{
    sqlite3_stmt *stmt;
    
    int pre_res = sqlite3_prepare(db,-1,&stmt,NULL);
    if (pre_res == YES) {
        return stmt;
    }
    return NULL;
}
// <2>.......
- (sqlite3_stmt*)execQueryWithsql:(Nsstring*)sql andWithParams:(NSArray*)params
{
    sqlite3_stmt *stmt;
    int pre_res = sqlite3_prepare_v2(db,NULL);
    if (pre_res == sqlITE_OK) {
        //绑定参数
        //先判断是否参数为nil,然后循环绑定多个参数
        if (params != nil)
        {
            for (int i = 0; i <[params count]; i++)
            {
                id obj = params[i];
                if (obj == nil) {
                    sqlite3_bind_null(stmt,i+1);
                }
                else if ([obj respondsToSelector:@selector(objCType)]){
                    //respondsToSelector判断是否有objCType方法,返回BOLL型
                    if (strstr("ilsILS",[obj objCType] )){
                        //strstr (char *,char*)判断是否在char*中出现过
                        sqlite3_bind_int(stmt,i+1,[obj intValue]);
                    }
                    else if (strstr("fdFD",[obj objCType])){
                        sqlite3_bind_double(stmt,[obj doubleValue]);
                    }
                    else{
                        stmt = nil;
                    }
                }
                else if([obj respondsToSelector:@selector(UTF8String)]){
                    sqlite3_bind_text(stmt,[obj UTF8String],NULL);
                    
                }else{
                    stmt = nil;
                }
            }
            return stmt;
        }
    }
    return NULL;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,320,568)];
    label.font = [UIFont systemFontOfSize:18];
    [label setNumberOfLines:0];
    label.textColor = [UIColor redColor];
    [self.view addSubview:label];
    
//    sqlite3 *db;//2. 声明sqlite3对象

    Nsstring *documentPath;
    documentPath = [self getUserDocumentPath];
    
    
    Nsstring *dbPath;
    dbPath = [self appendingPathComponent:documentPath andFileName:@"demo.sqlite"];
    NSLog(@"%@",dbPath);
    
   
    //5. 判断是否打开成功
    if ([self openorCreatesqliteDBWithDBPath:dbPath]) {
        NSLog(@"db is opened");
        
        //构造sql语句
        Nsstring *sql = @"create table if not exists tempYear(t_id integer primary key autoincrement,t_name varchar(20) not null,t_date date)";
        /*
           第一个参数: sqlite3 对象
           第二个参数: sql语句
           第三个参数: 回调函数
           第四个函数: 回调函数的参数
           第五个参数: 错误信息
         */
       int exec_res = sqlite3_exec(db,NULL);
       
        if (exec_res == sqlITE_OK) {
            NSLog(@"table is created");
        }
        //增加
        Nsstring *insert_sql = @"insert into tempYear(t_name) values('jeack')";
        if ([self execsqlNoQueryWith:insert_sql]) {
            NSLog(@"one recorder is inserted");
        }
        //删除
        Nsstring *delete_sql = @"delete from tempYear where t_id=2";
        if ([self execsqlNoQueryWith:delete_sql]) {
            NSLog(@"delete id already done");
        }
        //更改
        Nsstring *update_sql = @"update tempYear set t_name ='title' where t_id=4 " ;
        if ([self execsqlNoQueryWith:update_sql]) {
            NSLog(@"update id already done");
        }

        
        
        //查询
        Nsstring *search_sql = @"select * from tempYear where t_id>? and t_name like ?";//占位符
        
        int search_t_id=4;
        Nsstring *search_t_name = @"j%";
        
        //select * from user where username='admin' and pwd ='admin';
        
        
        sqlite3_stmt *stmt;
        /*>>>准备执行查询sql语句
          第一个参数:sqlite对象
          第二个参数:执行的sql语句
          第三个参数:sql语句的长度,通常用-1来代表(系统自己计算长度)也可以用strlen([search_sql UTF8String])函数来计算
          第四个参数:sqlite3_stmt对象
          第五个参数:未执行的sql语句
         
         */
//       int prepare_res = sqlite3_prepare_v2(db,[search_sql UTF8String],NULL);
        
       
//        if (prepare_res == sqlITE_OK) {
            /*
             sqlite3_bind_int(stmt,1,search_t_id);
                 绑定参数
             第一个参数:sqlite_stmt对象
             第二个参数:占位符索引,从1开始
             第三个参数:替代占位符的真实参数
             */

//            sqlite3_bind_int(stmt,search_t_id);
//            sqlite3_bind_text(stmt,2,[search_t_name UTF8String],NULL);
        
        NSArray *array = [NSArray arrayWithObjects:[NSNumber numberWithInt:search_t_id],search_t_name,nil];
        stmt = [self execQueryWithsql:search_sql andWithParams:array];
        
            while (sqlite3_step(stmt)==sqlITE_ROW) {
                int t_id = sqlite3_column_int(stmt,0);
                const unsigned char *t_name = sqlite3_column_text(stmt,1);
                NSLog(@"id=%d,name=%s",t_id,t_name);
        
            }
        //释放stmt
        sqlite3_finalize(stmt);
    }
    //关闭数据库
    sqlite3_close(db);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // dispose of any resources that can be recreated.
}

@end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


SQLite架构简单,又有Json计算能力,有时会承担Json文件/RESTful的计算功能,但SQLite不能直接解析Json文件/RESTful,需要用Java代码硬写,或借助第三方类库,最后再拼成insert语句插入数据表,代码非常繁琐,这里就不展示了。参考前面的代码可知,入库的过程比较麻烦,不能只用SQL,还要借助Java或命令行。SPL是现代的数据计算语言,属于简化的面向对象的语言风格,有对象的概念,可以用点号访问属性并进行多步骤计算,但没有继承重载这些内容,不算彻底的面向对象语言。...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。本教程将告诉您如何使用 SQLite 编程,并让你迅速上手。.................................
安卓开发,利用SQLite实现登陆注册功能
相比大多数数据库而言,具有等优势,广泛应用于、等领域。
有时候,一个项目只有一个数据库,比如只有SQLite,或者MySQL数据库,那么我们只需要使用一个固定的数据库即可。但是一个项目如果写好了,有多个用户使用,但是多个用户使用不同的数据库,这个时候,我们就需要把软件设计成可以连接多个数据库的模式,用什么数据库,就配置什么数据库即可。4.Users实体类,这个实体类要和数据库一样的,形成一一对应的关系。11.Sqlite数据库,需要在代码里面创建数据库,建立表,再建立数据。8.我们开启MySQL数据库,然后进行调试,看程序的结果。2.安装SqlSugar。
基于Android的背单词软件,功能强大完整。
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统。说白了就是使用起来轻便简单,