iPhone开发SQLite数据库使用

移动开发 iOS
本文介绍的iPhone开发SQLite数据库使用,主要是来介绍SQLite数据库在iPhone中使用。先来你看详细内容。

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数据库使用的内容介绍完了,希望本文对你有所帮助。

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-07-27 10:16:41

iPhone SQLite 数据库

2011-07-07 16:42:38

iPhone Sqlite3 数据库

2013-03-27 09:47:01

Android开发SQAndroid SDK

2011-08-05 16:31:47

iPhone 数据库

2019-08-15 07:00:54

SQLite数据库内存数据库

2011-07-21 17:29:42

iPhone Sqlite 数据库

2011-07-26 18:11:56

iPhone Sqlite 数据库

2013-04-01 10:49:51

iOS开发sqlite数据库

2023-10-17 08:31:03

SQLite数据库

2011-08-09 13:22:31

iPhoneSqlite数据库

2011-07-21 15:05:14

iPhone 数据库

2011-07-20 12:34:49

SQLite数据库约束

2017-07-12 09:20:42

SQLite数据库移植

2021-09-12 17:25:12

SQLite数据库

2009-09-14 13:57:37

SQLite数据库开发

2011-08-04 18:00:47

SQLite数据库批量数据

2011-08-02 16:43:26

iPhone开发 Ssqlite3 数据库

2024-02-28 08:06:17

2011-07-25 14:14:49

iPhone SQLITE Pldatabase

2011-08-11 17:00:33

iPhone数据库SQLite
点赞
收藏

51CTO技术栈公众号