iPhone開發SQLite資料庫使用

來源:互聯網
上載者:User

iPhone開發SQLite資料庫使用是本文要介紹的內容,我現在要使用SQLite 3.0建立一個資料庫,然後在資料庫中建立一個表格。首先要引入SQLite 3.0的lib庫。然後包含標頭檔#import

1、開啟資料庫,如果沒有,那麼建立一個

 
  1. sqlite3* database_;  
  2.  
  3. -(BOOL) open  
  4.  
  5. {  
  6.        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  7.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  8.     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydb.sql"];  
  9.     NSFileManager *fileManager = [NSFileManager defaultManager];  
  10.     BOOL find = [fileManager fileExistsAtPath:path];  
  11.  
  12.     //找到資料庫檔案mydb.sql  
  13.     if (find) {  
  14.         NSLog(@"Database file have already existed.");  
  15.         if(sqlite3_open([path UTF8String], &database_) != SQLITE_OK) {  
  16.             sqlite3_close(database_);  
  17.             NSLog(@"Error: open database file.");  
  18.             return NO;  
  19.         }  
  20.         return YES;  
  21.     }  
  22.     if(sqlite3_open([path UTF8String], &database_) == SQLITE_OK) {  
  23.         bFirstCreate_ = YES;  
  24.         [self createChannelsTable:database_];//在後面實現函數createChannelsTable  
  25.  
  26.         return YES;  
  27.     } else {  
  28.         sqlite3_close(database_);  
  29.         NSLog(@"Error: open database file.");  
  30.         return NO;  
  31.     }  
  32.     return NO;  

2、建立表格

 
  1. //建立表格,假設有五個欄位,id,cid,title,imageData ,imageLen )  
  2. //說明一下,id為表格的主鍵,必須有。  
  3. //cid,和title都是字串,imageData是位元據,imageLen 是該位元據的長度。  
  4. - (BOOL) createChannelsTable:(sqlite3*)db{  
  5.     char *sql = "CREATE TABLE channels (id integer primary key, \  
  6.                                         cid text, \  
  7.                                         title text, \  
  8.                                         imageData BLOB, \  
  9.                                         imageLen integer)";  
  10.     sqlite3_stmt *statement;  
  11.     if(sqlite3_prepare_v2(db, sql, -1, &statement, nil) != SQLITE_OK) {  
  12.         NSLog(@"Error: failed to prepare statement:create channels table");  
  13.         return NO;  
  14.     }  
  15.     int success = sqlite3_step(statement);  
  16.     sqlite3_finalize(statement);  
  17.     if ( success != SQLITE_DONE) {  
  18.         NSLog(@"Error: failed to dehydrate:CREATE TABLE channels");  
  19.         return NO;  
  20.     }  
  21.     NSLog(@"Create table 'channels' successed.");  
  22.     return YES;  

3、向表格中插入一條記錄

假設channle是一個資料結構體,儲存了一條記錄的內容。

 
  1. - (BOOL) insertOneChannel:(Channel*)channel{  
  2.     NSData* ImageData = UIImagePNGRepresentation( channel.image_);  
  3.     NSInteger Imagelen = [ImageData length];  
  4.     sqlite3_stmt *statement;  
  5.     static char *sql = "INSERT INTO channels (cid,title,imageData,imageLen)\  
  6.                         VALUES(?,?,?,?)";  
  7.  
  8.     //問號的個數要和(cid,title,imageData,imageLen)裡面欄位的個數匹配,代表未知的值,將在下面將值和欄位關聯。  
  9.     int success = sqlite3_prepare_v2(database_, sql, -1, &statement, NULL);  
  10.     if (success != SQLITE_OK) {  
  11.         NSLog(@"Error: failed to insert:channels");  
  12.         return NO;  
  13.     }  
  14.       
  15.  
  16.    //這裡的數字1,2,3,4代表第幾個問號  
  17.     sqlite3_bind_text(statement, 1, [channel.id_ UTF8String], -1, SQLITE_TRANSIENT);  
  18.     sqlite3_bind_text(statement, 2, [channel.title_ UTF8String], -1, SQLITE_TRANSIENT);  
  19.     sqlite3_bind_blob(statement, 3, [ImageData bytes], Imagelen, SQLITE_TRANSIENT);  
  20.     sqlite3_bind_int(statement, 4, Imagelen);      
  21.  
  22.  
  23.     success = sqlite3_step(statement);  
  24.     sqlite3_finalize(statement);  
  25.       
  26.     if (success == SQLITE_ERROR) {  
  27.         NSLog(@"Error: failed to insert into the database with message.");  
  28.         return NO;  
  29.     }   
  30.    
  31.   NSLog(@"Insert One Channel#############:id = %@",channel.id_);  
  32.     return YES;  

4、資料庫查詢

這裡擷取表格中所有的記錄,放到數組fChannels中。

 
  1. - (void) getChannels:(NSMutableArray*)fChannels{  
  2.     sqlite3_stmt *statement = nil;  
  3.     char *sql = "SELECT * FROM channels";  
  4.     if (sqlite3_prepare_v2(database_, sql, -1, &statement, NULL) != SQLITE_OK) {  
  5.         NSLog(@"Error: failed to prepare statement with message:get channels.");  
  6.     }  
  7.     //查詢結果集中一條一條的遍曆所有的記錄,這裡的數字對應的是列值。  
  8.     while (sqlite3_step(statement) == SQLITE_ROW) {  
  9.         char* cid       = (char*)sqlite3_column_text(statement, 1);  
  10.         char* title     = (char*)sqlite3_column_text(statement, 2);  
  11.         Byte* imageData = (Byte*)sqlite3_column_blob(statement, 3);  
  12.         int imageLen    = sqlite3_column_int(statement, 4);          
  13.         Channel* channel = [[Channel alloc] init];  
  14.         if(cid)  
  15.             channel.id_ = [NSString stringWithUTF8String:cid];  
  16.         if(title)  
  17.             channel.title_ = [NSString stringWithUTF8String:title];  
  18.         if(imageData){  
  19.             UIImage* image = [UIImage imageWithData:[NSData dataWithBytes:imageData length:imageLen]];  
  20.             channel.image_ = image;  
  21.         }  
  22.          [fChannels addObject:channel];  
  23.         [channel release];  
  24.     }  
  25.     sqlite3_finalize(statement);  

小結:iPhone開發SQLite資料庫使用的內容介紹完了,希望本文對你有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.