sqlite3的简单使用

Android中也有sqlite3,主要是纪录一些基础的语法,ios提供封装的类,不需要下面这样操作

1,创建数据库

#pragma mark-打开数据库

-(BOOL)openDataBase{
    //1,存放路径,存放在当前应用的沙盒中 document
    
    //    NSArray *arrayPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //
    //    NSString *documentPath= [arrayPath objectAtIndex:0];//document路径
    
    NSString *documentPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    //数据库路径
    NSString *sqlPath=[documentPath  stringByAppendingPathComponent:@"applicationData.db"];
    
    //打开数据库
    //sqlPath.UTF8String转换成c字符串 ,sqlite3的指针
    
    int result=sqlite3_open(sqlPath.UTF8String,&db);
    
    if (result==SQLITE_OK) {
        
         [self showDialogError:@"数据库打开" andContent:@"成功"];
        return true;
    }else{
        
        sqlite3_close(db);
        return false;
        
    }
    

}

2,创建数据库表

#pragma mark-创建数据库表

-(BOOL)createTable:(NSMutableDictionary*) createTableRowParams{
    
    char *sql;
    //创建sql语句
    
    if ([createTableRowParams count]>0) {
        
        
    }else{
    
    sql= "create table if not exists UCS(id integer primary key,loginTime text,page text,pageCount integer);";
    
    }
    
    char *error=nil;
    //执行sql语句
    sqlite3_exec(db, sql, nil, nil, &error);
    
    
    if (error==nil) {
        NSLog(@"创建表成功。。");
    }else{
    
        NSLog(@"创建表失败。。");
    }
    
    return true;
}

3,插入数据:

////插入数据
-(void)insertTableData:(NSArray*)arrayData{
    NSDate * senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    
   [dateformatter setDateFormat:@"YYYYMMddHHmmss"];
    NSString * morelocationString=[dateformatter stringFromDate:senddate];

    char *error=nil;
    
    for(int n=0;n<10;n++){
    
//    NSString *sql=[[NSString alloc]initWithFormat:@"insert into UCS(loginTime,page) values(?,?)",morelocationString,@"baihe"];
//        char *sql1 = @"insert into UCS(loginTime,page) values(?,?)";
//        
//    sqlite3_exec(db, sql1, NULL, NULL, &error);
//        
//    }
        
        
        char *sql="insert into UCS(loginTime,page,pageCount) values(?,?,?)";
        
        sqlite3_stmt *stmt=nil;
        
        if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL)==SQLITE_OK) {
            
            sqlite3_bind_text(stmt, 1, "loginTimedata", -1, NULL);
            sqlite3_bind_text(stmt, 2, "pagedata", -1, NULL);
            sqlite3_bind_int(stmt, 3, 100);
            
        }
        
        
        if (sqlite3_step(stmt)!=SQLITE_DONE) {
            
            NSLog(@"插入数据失败。");

        }else{
         NSLog(@"插入数据成功");
        
        }
        
        
  sqlite3_finalize(stmt);//释放
    
    }

}

查询数据:

//查询
-(void)selectTableData{
    
//    NSString *sql=@"select * from UCS";
    char *sql="select * from UCS;";
    
    
    //
    sqlite3_stmt *ppStmt;
    
    
    //单步执行语句
    if (sqlite3_prepare_v2(db, sql, -1, &ppStmt, NULL) == SQLITE_OK) {
        while (sqlite3_step(ppStmt)==SQLITE_ROW) {
            
            int id5=sqlite3_column_int(ppStmt, 0);
            
            char *cc=(char *)sqlite3_column_text(ppStmt, 2);
            int id6=sqlite3_column_int(ppStmt, 3);

            NSString *loginTime=[NSString stringWithUTF8String:cc];

            NSLog(@"id=%d,loginTime=%@ %d",id5,loginTime,id6);
        }
    }
    

    
    sqlite3_finalize(ppStmt);//释放

}

删除和更新类似

相关推荐