• ios中Pldatabase的用法(3)


    #import "ViewController.h"
    
    @interface ViewController ()
    @property(nonatomic,retain)PLSqliteDatabase *db;
    @end
    
    @implementation ViewController
    
    
    #pragma mark -生命周期方法
    -(void)dealloc{
        self.db=nil;
        [super dealloc];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self OpenDb];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark -事件
    - (IBAction)createTable:(id)sender {
        [self createTable];
    }
    
    - (IBAction)ExtistTable:(id)sender {
    }
    
    - (IBAction)Insert:(id)sender {
        [self InsertData];
    }
    
    - (IBAction)update:(id)sender {
        [self UpdateData];
    }
    
    - (IBAction)delete1:(id)sender {
        [self deletedata];
    }
    - (IBAction)select1:(id)sender {
        [self selectdata1];
    }
    
    #pragma mark--数据库操作
    -(void)OpenDb{
       NSString *path= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        path=[path stringByAppendingPathComponent:@"test.db"];
        //open db
        PLSqliteDatabase *sqldb=[[PLSqliteDatabase alloc] initWithPath:path];//没有数据库,会创建一个的
    
        if(![sqldb open]){
            NSLog(@"没有打开数据库");
            
        }
        self.db=sqldb;
     
        [sqldb release];
    }
    
    -(void)createTable{
        if (![self.db tableExists:@"TEST"]) {
            if(![self.db executeUpdate:@"CREATE TABLE TEST (id integer primary key autoincrement,name text,age integer)"]){
                NSLog(@"create table faild");
            }
        }
        else{
            NSLog(@"表已经存在");
        }
       
    }
    
    -(void)InsertData{
       NSString *sql=@"INSERT INTO TEST(name,age) values(?,?)";
        
        if([self.db executeUpdate:sql,@"gcb",@12]){//注意参数必须是oc对象
            NSLog(@"insert success");
        }
        else{
            NSLog(@"insert failed");
        }
        
    }
    
    -(void)UpdateData{
      NSString *sql=@"UPDATE TEST SET name=?,age=? where id=?";
        if([self.db executeUpdate:sql,@"ios",@2013,@2]){//注意参数必须是oc对象
            NSLog(@"update success");
        }
        else{
            NSLog(@"updata failed");
        }
    }
    
    -(void)selectdata{
         NSString *sql=@"SELECT id,name,age FROM TEST WHERE id=?";
        id<PLResultSet> result=[self.db executeQuery:sql,@1];
        while ([result next]) {
        int pid=[result intForColumn:@"id"];
            NSString *name=[result stringForColumn:@"name"];
            int32_t age=[result intForColumn:@"age"];
            NSLog(@"id=%zi,name=%@,age=%zi",pid,name,age);
        }
    }
    
    -(void)selectdata1{
       // NSString *sql=@"SELECT id,name,age FROM TEST WHERE (id like '%%%@%%' OR age like '%%@%%' ";
        NSString *sql=[NSString stringWithFormat:@"SELECT id,name,age FROM TEST WHERE (id like'%%%@%%' OR age like '%%%@%%')",@1,@2];
        
        id<PLResultSet> result=[self.db executeQuery:sql];
        while ([result next]) {
            int pid=[result intForColumn:@"id"];
            NSString *name=[result stringForColumn:@"name"];
            int32_t age=[result intForColumn:@"age"];
            NSLog(@"id=%zi,name=%@,age=%zi",pid,name,age);
        }
    }
    
    -(void)deletedata{
        NSString *sql=@"DELETE FROM TEST WHERE id=?";
        if([self.db executeUpdate:sql,@1]){
            NSLog(@"delete success");
        }
        else{
            NSLog(@"delete failed");
        }
    }

     插入的另一种方法(如果是要封装,可以考虑下面)

    -(void)InsertData1{
        NSString *sql=@"INSERT INTO TEST(name,age) values(:name,:age)";
        
        id<PLPreparedStatement> stm=[self.db prepareStatement:sql];
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:@"tt" forKey:@"name"];
        [dic setObject:@12 forKey:@"age"];
        [stm bindParameterDictionary:dic];
        
        if([stm executeUpdate]){
            NSLog(@"ss");
        }
        
    -(void) selectdate2{
        NSString *sql=@"SELECT id,name,age FROM TEST WHERE id=:id";
        id<PLPreparedStatement> stmp=[self.db prepareStatement:sql];
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        [dic setObject:@1 forKey:@"id"];
        [stmp bindParameterDictionary:dic];
        id<PLResultSet> result=[stmp executeQuery];
        while ([result next]) {
            int pid=[result intForColumn:@"id"];
            NSString *name=[result stringForColumn:@"name"];
            int age=[result  intForColumn:@"age"];
            NSLog(@"name=%@,age=%zi,id=%zi",name,age,pid);
        }
    }
  • 相关阅读:
    PHP $_SERVER['HTTP_REFERER'] 获取前一页面的 URL 地址
    LAMP与LNMP架构的区别及其具体的选择说明
    LNMP 与 LAMP 架构的区别及配置解决方案
    LAMP和LNMP,你更愿意选择谁,为什么?
    Storm流计算从入门到精通之技术篇(高并发策略、批处理事务、Trident精解、运维监控、企业场景)
    Zookeeper从入门到精通(开发详解,案例实战,Web界面监控)
    基于Greenplum Hadoop分布式平台的大数据解决方案及商业应用案例剖析
    深入浅出Hive企业级架构优化、Hive Sql优化、压缩和分布式缓存(企业Hadoop应用核心产品)
    深入浅出OpenStack云计算平台管理(nova-compute/network)
    玩转大数据:深入浅出大数据挖掘技术(Apriori算法、Tanagra工具、决策树)
  • 原文地址:https://www.cnblogs.com/gcb999/p/3200309.html
Copyright © 2020-2023  润新知