偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

iPhone開發(fā)SQLite數(shù)據(jù)庫(kù)使用

移動(dòng)開發(fā) iOS
本文介紹的iPhone開發(fā)SQLite數(shù)據(jù)庫(kù)使用,主要是來(lái)介紹SQLite數(shù)據(jù)庫(kù)在iPhone中使用。先來(lái)你看詳細(xì)內(nèi)容。

iPhone開發(fā)SQLite數(shù)據(jù)庫(kù)使用是本文要介紹的內(nèi)容,我現(xiàn)在要使用SQLite 3.0創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),然后在數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)表格。首先要引入SQLite 3.0的lib庫(kù)。然后包含頭文件#import

1、打開數(shù)據(jù)庫(kù),如果沒(méi)有,那么創(chuàng)建一個(gè)

  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.     //找到數(shù)據(jù)庫(kù)文件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_];//在后面實(shí)現(xiàn)函數(shù)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、創(chuàng)建表格

  1. //創(chuàng)建表格,假設(shè)有五個(gè)字段,(id,cid,title,imageData ,imageLen )  
  2. //說(shuō)明一下,id為表格的主鍵,必須有。  
  3. //cid,和title都是字符串,imageData是二進(jìn)制數(shù)據(jù),imageLen 是該二進(jìn)制數(shù)據(jù)的長(zhǎng)度。  
  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、向表格中插入一條記錄

假設(shè)channle是一個(gè)數(shù)據(jù)結(jié)構(gòu)體,保存了一條記錄的內(nèi)容。

  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.     //問(wèn)號(hào)的個(gè)數(shù)要和(cid,title,imageData,imageLen)里面字段的個(gè)數(shù)匹配,代表未知的值,將在下面將值和字段關(guān)聯(lián)。  
  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.    //這里的數(shù)字1,2,3,4代表第幾個(gè)問(wèn)號(hào)  
  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、數(shù)據(jù)庫(kù)查詢

這里獲取表格中所有的記錄,放到數(shù)組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.     //查詢結(jié)果集中一條一條的遍歷所有的記錄,這里的數(shù)字對(duì)應(yīng)的是列值。  
  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);  

小結(jié):iPhone開發(fā)SQLite數(shù)據(jù)庫(kù)使用的內(nèi)容介紹完了,希望本文對(duì)你有所幫助。

責(zé)任編輯:zhaolei 來(lái)源: 博客園
相關(guān)推薦

2011-07-27 10:16:41

iPhone SQLite 數(shù)據(jù)庫(kù)

2011-07-07 16:42:38

iPhone Sqlite3 數(shù)據(jù)庫(kù)

2011-08-05 16:31:47

iPhone 數(shù)據(jù)庫(kù)

2013-03-27 09:47:01

Android開發(fā)SQAndroid SDK

2011-07-21 17:29:42

iPhone Sqlite 數(shù)據(jù)庫(kù)

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫(kù)

2019-08-15 07:00:54

SQLite數(shù)據(jù)庫(kù)內(nèi)存數(shù)據(jù)庫(kù)

2013-04-01 10:49:51

iOS開發(fā)sqlite數(shù)據(jù)庫(kù)

2023-10-17 08:31:03

SQLite數(shù)據(jù)庫(kù)

2011-08-09 13:22:31

iPhoneSqlite數(shù)據(jù)庫(kù)

2011-07-21 15:05:14

iPhone 數(shù)據(jù)庫(kù)

2011-07-20 12:34:49

SQLite數(shù)據(jù)庫(kù)約束

2017-07-12 09:20:42

SQLite數(shù)據(jù)庫(kù)移植

2021-09-12 17:25:12

SQLite數(shù)據(jù)庫(kù)

2011-08-02 16:43:26

iPhone開發(fā) Ssqlite3 數(shù)據(jù)庫(kù)

2009-09-14 13:57:37

SQLite數(shù)據(jù)庫(kù)開發(fā)

2011-08-04 18:00:47

SQLite數(shù)據(jù)庫(kù)批量數(shù)據(jù)

2011-07-25 14:14:49

iPhone SQLITE Pldatabase

2024-10-28 16:31:03

2011-08-11 17:00:33

iPhone數(shù)據(jù)庫(kù)SQLite
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)