1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| -(BOOL)openDataBaeWithName:(NSString *)path{ if (sqlite3_open([path UTF8String], &_sqlite3_db)!=SQLITE_OK) { sqlite3_close(_sqlite3_db); _sqlite3_db=nil; return NO; }else{ return YES; } } -(void)createTableWithName:(NSString *)name keysDictionary:(NSDictionary<NSString *,NSString *> *)dic callBack:(void (^)(YHBaseSQLError *))complete{ NSMutableString * keys = [[NSMutableString alloc]init]; for (int i=0; i<dic.allKeys.count; i++) { NSString * key = dic.allKeys[i]; if (i<dic.allKeys.count-1) { [keys appendFormat:@"%@ %@,",key,[dic objectForKey:key]]; }else{ [keys appendFormat:@"%@ %@",key,[dic objectForKey:key]]; } } NSString * sqlStr = [NSString stringWithFormat:@"create table %@(%@)",name,keys]; [self runSQL:sqlStr callBack:^(YHBaseSQLError * error) { if (complete) { complete(error); } }]; } -(void)insertData:(NSDictionary<NSString *,id> *)dataDic intoTable:(NSString *)name callBack:(void (^)(YHBaseSQLError *))complete{ NSMutableString * keys = [[NSMutableString alloc]init]; NSMutableString * values = [[NSMutableString alloc]init]; for (int i=0; i<dataDic.allKeys.count; i++) { NSString * key = dataDic.allKeys[i]; if (i<dataDic.count-1) { [keys appendFormat:@"%@,",key]; [values appendFormat:@"\"%@\",",[dataDic objectForKey:key]]; }else{ [keys appendFormat:@"%@",key]; [values appendFormat:@"\"%@\"",[dataDic objectForKey:key]]; } } NSString * sqlStr = [NSString stringWithFormat:@"insert into %@(%@) values(%@)",name,keys,values]; [self runSQL:sqlStr callBack:^(YHBaseSQLError *error) { if (complete) { complete(error); } }]; } -(void)addKey:(NSString *)kName keyType:(NSString *)type intoTable:(NSString *)tableName callBack:(void (^)(YHBaseSQLError *))complete{ NSString * sqlStr = [NSString stringWithFormat:@"alter table %@ add %@ %@",tableName,kName,type]; [self runSQL:sqlStr callBack:^(YHBaseSQLError *error) { if (complete) { complete(error); } }]; } -(void)update:(NSDictionary<NSString *,id> *)dataDic inTable:(NSString *)tableName whileString:(NSString *)wlStr callBack:(void (^)(YHBaseSQLError *))complete{ NSMutableString * sqlStr = [[NSMutableString alloc]init]; [sqlStr appendFormat:@"update %@ set ",tableName]; for (int i=0; i<dataDic.allKeys.count; i++) { NSString * key = dataDic.allKeys[i]; if (i<dataDic.allKeys.count-1) { [sqlStr appendFormat:@"%@=\"%@\",",key,[dataDic objectForKey:key]]; }else{ [sqlStr appendFormat:@"%@=\"%@\"",key,[dataDic objectForKey:key]]; if (wlStr!=nil) { [sqlStr appendFormat:@" where %@",wlStr]; } } } [self runSQL:sqlStr callBack:^(YHBaseSQLError *error) { if (complete) { complete(error); } }]; }
-(void)deleteDataFromTable:(NSString *)tableName whereString:(NSString *)wlStr callBack:(void (^)(YHBaseSQLError *))complete{ NSMutableString * sqlStr = [[NSMutableString alloc]init]; [sqlStr appendFormat:@"delete from %@",tableName]; if (wlStr!=nil) { [sqlStr appendFormat:@" where %@",wlStr]; } [self runSQL:sqlStr callBack:^(YHBaseSQLError *error) { if (complete) { complete(error); } }]; } -(void)dropTable:(NSString *)tableName callBack:(void (^)(YHBaseSQLError *))complete{ NSString * sqlStr = [NSString stringWithFormat:@"drop table %@",tableName]; [self runSQL:sqlStr callBack:^(YHBaseSQLError *error) { if (complete) { complete(error); } }]; } -(void)selectKeys:(NSArray<NSDictionary *> *)keys fromTable:(NSString *)tableName orderBy:(NSString *)orderKey orderType:(NSString *)type whileStr:(NSString *)wlstr callBack:(void (^)(NSArray<NSDictionary *> *, YHBaseSQLError *))complete{ NSMutableString * sqlStr = [[NSMutableString alloc]init]; [sqlStr appendFormat:@"select"]; if (keys==nil||keys.count==0) { [sqlStr appendFormat:@" * from %@",tableName]; }else{ for (int i=0; i<keys.count; i++) { if (i<keys.count-1) { [sqlStr appendFormat:@" %@,",keys[i].allKeys.firstObject]; }else{ [sqlStr appendFormat:@" %@ from %@",keys[i].allKeys.firstObject,tableName]; } } } if (wlstr) { [sqlStr appendFormat:@" where %@",wlstr]; } if (orderKey) { [sqlStr appendFormat:@" order by %@",orderKey]; } if (type) { [sqlStr appendFormat:@" %@",type]; } NSMutableArray * keysArr = [[NSMutableArray alloc]init]; NSMutableArray * keysTypeArr = [[NSMutableArray alloc]init]; if (keys==nil||keys.count==0) { NSArray<NSDictionary *> * tmpArr = [self getTheTableAllKeys:tableName]; for (int i=0; i<tmpArr.count; i++) { NSString * key = tmpArr[i].allKeys.firstObject; [keysArr addObject:key]; [keysTypeArr addObject:[tmpArr[i] objectForKey:key]]; } }else{ for (int i=0; i<keys.count; i++) { NSString * key = keys[i].allKeys.firstObject; [keysArr addObject:key]; [keysTypeArr addObject:[keys[i] objectForKey:key]]; } } [self runSelectSQL:sqlStr withKeys:keysArr withDataType:keysTypeArr callBack:^(NSArray<NSDictionary *> *dataArray, YHBaseSQLError *error) { if (complete) { complete(dataArray,error); } }]; } -(void)closeContext{ sqlite3_close(_sqlite3_db); _sqlite3_db = nil; }
//内部方法 运行创建独立的非查询SQL语句 -(void)runSQL:(NSString *)sql callBack:(void(^)(YHBaseSQLError * error))complete{ char * err; int code = sqlite3_exec(_sqlite3_db, [sql UTF8String], NULL, NULL, &err); if (code!=SQLITE_OK) { YHBaseSQLError * error = [[YHBaseSQLError alloc]init]; error.errorInfo = [NSString stringWithCString:err encoding:NSUTF8StringEncoding]; error.errorCode = code; complete(error); }else{ complete(nil); } } //运行查询语句 -(void)runSelectSQL:(NSString *)sql withKeys:(NSArray *)keys withDataType:(NSArray *)dataType callBack:(void(^)(NSArray<NSDictionary *> * dataArray, YHBaseSQLError * error))complete{ sqlite3_stmt *stmt =nil; int code = sqlite3_prepare_v2(_sqlite3_db, [sql UTF8String], -1, &stmt, NULL); if (code!=SQLITE_OK) { YHBaseSQLError * error = [[YHBaseSQLError alloc]init]; error.errorInfo = @"查询失败"; error.errorCode=code; complete(nil,error); }else{ NSMutableArray * resultArray = [[NSMutableArray alloc]init]; while (sqlite3_step(stmt)==SQLITE_ROW) { //数据类型的分别解析 NSMutableDictionary * dic = [[NSMutableDictionary alloc]init]; for (int i=0; i<dataType.count; i++) { NSString * type = dataType[i]; if ([type isEqualToString:YHBASE_SQL_DATATYPE_BINARY]) { int length = sqlite3_column_bytes(stmt, i); const void *data = sqlite3_column_blob(stmt, i); NSData * value = [NSData dataWithBytes:data length:length]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_BLOB]){ int length = sqlite3_column_bytes(stmt, i); const void *data = sqlite3_column_blob(stmt, i); NSData * value = [NSData dataWithBytes:data length:length]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_BOOLEAN]){ NSNumber * value = [NSNumber numberWithInt:sqlite3_column_int(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_CURRENCY]){ NSNumber * value = [NSNumber numberWithLong:sqlite3_column_int64(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_DATE]){ char * cString =(char*)sqlite3_column_text(stmt, i); NSString * value = [NSString stringWithCString:cString?cString:"NULL" encoding:NSUTF8StringEncoding]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_DOUBLE]){ NSNumber * value = [NSNumber numberWithFloat:sqlite3_column_double(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_FLOAT]){ NSNumber * value = [NSNumber numberWithFloat:sqlite3_column_double(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_INTRGER]){ NSNumber * value = [NSNumber numberWithInt:sqlite3_column_int(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_REAL]){ NSNumber * value = [NSNumber numberWithDouble:sqlite3_column_int(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_SMALLINT]){ NSNumber * value = [NSNumber numberWithShort:sqlite3_column_int(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_TEXT]){ char * cString =(char*)sqlite3_column_text(stmt, i); NSString * value = [NSString stringWithCString:cString?cString:"NULL" encoding:NSUTF8StringEncoding]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_TIME]){ char * cString =(char*)sqlite3_column_text(stmt, i); NSString * value = [NSString stringWithCString:cString?cString:"NULL" encoding:NSUTF8StringEncoding]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_TIMESTAMP]){ NSNumber * value = [NSNumber numberWithLongLong:sqlite3_column_int64(stmt, i)]; [dic setObject:value forKey:keys[i]]; }else if([type isEqualToString:YHBASE_SQL_DATATYPE_VARCHAR]){ char * cString =(char*)sqlite3_column_text(stmt, i); NSString * value = [NSString stringWithCString:cString?cString:"NULL" encoding:NSUTF8StringEncoding]; [dic setObject:value forKey:keys[i]]; } } [resultArray addObject:dic]; } sqlite3_finalize(stmt); stmt=nil; complete(resultArray,nil); } } //获取表中所有字段名和类型 -(NSArray<NSDictionary *> *)getTheTableAllKeys:(NSString *)tableName{ NSMutableArray * array = [[NSMutableArray alloc]init]; NSString * getColumn = [NSString stringWithFormat:@"PRAGMA table_info(%@)",tableName]; sqlite3_stmt *statement; sqlite3_prepare_v2(_sqlite3_db, [getColumn UTF8String], -1, &statement, nil); while (sqlite3_step(statement) == SQLITE_ROW) { char *nameData = (char *)sqlite3_column_text(statement, 1); NSString *columnName = [[NSString alloc] initWithUTF8String:nameData]; char *typeData = (char *)sqlite3_column_text(statement, 2); NSString *columntype = [NSString stringWithCString:typeData encoding:NSUTF8StringEncoding]; NSDictionary * dic = @{columnName:columntype}; [array addObject:dic]; } sqlite3_finalize(statement); statement=nil; return array; }
|