Revision: 25510
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 1, 2010 05:01 by iswear_wxp
Initial Code
我现在è¦ä½¿ç”¨SQLite3.0创建一个数æ®åº“,然åŽåœ¨æ•°æ®åº“ä¸åˆ›å»ºä¸€ä¸ªè¡¨æ ¼ã€‚ 首先è¦å¼•å…¥SQLite3.0çš„lib库。然åŽåŒ…å«å¤´æ–‡ä»¶#import <sqlite3.h> ã€1】打开数æ®åº“,如果没有,那么创建一个 sqlite3* database_; -(BOOL) open{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sql"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL find = [fileManager fileExistsAtPath:path]; // 找到数æ®åº“文件mydb.sql if (find) { NSLog(@"Database file have already existed."); if(sqlite3_open([path UTF8String], &database_) != SQLITE_OK) { sqlite3_close(database_); NSLog(@"Error: open database file."); return NO; } return YES; } if(sqlite3_open([path UTF8String], &database_) == SQLITE_OK) { bFirstCreate_ = YES; [self createChannelsTable:database_];//在åŽé¢å®žçŽ°å‡½æ•°createChannelsTable return YES; } else { sqlite3_close(database_); NSLog(@"Error: open database file."); return NO; } return NO; } ã€2ã€‘åˆ›å»ºè¡¨æ ¼ //åˆ›å»ºè¡¨æ ¼ï¼Œå‡è®¾æœ‰äº”个å—段,(id,cid,title,imageData ,imageLen ) //说明一下,idä¸ºè¡¨æ ¼çš„ä¸»é”®ï¼Œå¿…é¡»æœ‰ã€‚ //cid, å’Œtitle都是å—符串,imageData是二进制数æ®ï¼ŒimageLen 是该二进制数æ®çš„长度。 - (BOOL) createChannelsTable:(sqlite3*)db{ char *sql = "CREATE TABLE channels (id integer primary key, \ cid text, \ title text, \ imageData BLOB, \ imageLen integer)"; sqlite3_stmt *statement; if(sqlite3_prepare_v2(db, sql, -1, &statement, nil) != SQLITE_OK) { NSLog(@"Error: failed to prepare statement:create channels table"); return NO; } int success = sqlite3_step(statement); sqlite3_finalize(statement); if ( success != SQLITE_DONE) { NSLog(@"Error: failed to dehydrate:CREATE TABLE channels"); return NO; } NSLog(@"Create table 'channels' successed."); return YES; } ã€3】å‘è¡¨æ ¼ä¸æ’入一æ¡è®°å½• å‡è®¾channle是一个数æ®ç»“构体,ä¿å˜äº†ä¸€æ¡è®°å½•çš„内容。 - (BOOL) insertOneChannel:(Channel*)channel{ NSData* ImageData = UIImagePNGRepresentation( channel.image_); NSInteger Imagelen = [ImageData length]; sqlite3_stmt *statement; static char *sql = "INSERT INTO channels (cid,title,imageData,imageLen)\ VALUES(?,?,?,?)"; //é—®å·çš„个数è¦å’Œ(cid,title,imageData,imageLen)里é¢å—段的个数匹é…,代表未知的值,将在下é¢å°†å€¼å’Œå—段关è”。 int success = sqlite3_prepare_v2(database_, sql, -1, &statement, NULL); if (success != SQLITE_OK) { NSLog(@"Error: failed to insert:channels"); return NO; } //这里的数å—1,2,3,4ä»£è¡¨ç¬¬å‡ ä¸ªé—®å· sqlite3_bind_text(statement, 1, [channel.id_ UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(statement, 2, [channel.title_ UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_blob(statement, 3, [ImageData bytes], Imagelen, SQLITE_TRANSIENT); sqlite3_bind_int(statement, 4, Imagelen); success = sqlite3_step(statement); sqlite3_finalize(statement); if (success == SQLITE_ERROR) { NSLog(@"Error: failed to insert into the database with message."); return NO; } NSLog(@"Insert One Channel#############:id = %@",channel.id_); return YES; } ã€4】数æ®åº“查询 这里获å–è¡¨æ ¼ä¸æ‰€æœ‰çš„记录,放到数组fChannelsä¸ã€‚ - (void) getChannels:(NSMutableArray*)fChannels{ sqlite3_stmt *statement = nil; char *sql = "SELECT * FROM channels"; if (sqlite3_prepare_v2(database_, sql, -1, &statement, NULL) != SQLITE_OK) { NSLog(@"Error: failed to prepare statement with message:get channels."); } //查询结果集ä¸ä¸€æ¡ä¸€æ¡çš„é历所有的记录,这里的数å—对应的是列值。 while (sqlite3_step(statement) == SQLITE_ROW) { char* cid = (char*)sqlite3_column_text(statement, 1); char* title = (char*)sqlite3_column_text(statement, 2); Byte* imageData = (Byte*)sqlite3_column_blob(statement, 3); int imageLen = sqlite3_column_int(statement, 4); Channel* channel = [[Channel alloc] init]; if(cid) channel.id_ = [NSString stringWithUTF8String:cid]; if(title) channel.title_ = [NSString stringWithUTF8String:title]; if(imageData){ UIImage* image = [UIImage imageWithData:[NSData dataWithBytes:imageData length:imageLen]]; channel.image_ = image; } [fChannels addObject:channel]; [channel release]; } sqlite3_finalize(statement); } 就简å•è¯´è¿™äº›å§ã€‚
Initial URL
Initial Description
Initial Title
iphoneå¼€å‘-SQLiteæ•°æ®åº“使用
Initial Tags
Initial Language
iPhone